facebook php sdk error - 必须使用活动访问令牌来查询有关当前用户的信息

时间:2023-01-22 17:42:20
FacebookSession::setDefaultApplication('1578373582426403', 'CENSORED');
$session = FacebookSession::newAppSession('1578373582426403', 'CENSORED');

$request = new FacebookRequest($session, 'GET', '/me');

$response = $request->execute();

$graphObject = $response->getGraphObject();

when $request->execute() is called, i get the error:

当$ request-> execute()被调用时,我得到错误:

An active access token must be used to query information about the current user.

必须使用活动访问令牌来查询有关当前用户的信息。

Im not sure what im doing wrong... can anyone help?

我不确定我做错了什么......任何人都可以帮忙吗?

2 个解决方案

#1


If you are trying to access the current user's information,try this. Change the redirect login

如果您尝试访问当前用户的信息,请尝试此操作。更改重定向登录

// Call Facebook Session- App Id, App Secret
Facebook\FacebookSession::setDefaultApplication('1578373582426403', 'CENSORED');

//Facebook Redirect Helper
//Create a FB variable, Pass oAuth URL
$facebook= new Facebook\FacebookRedirectLoginHelper('http://localhost/fb/');

//
try
{
    //To check method getSessionFromRedirect
    if($session= $facebook->getSessionFromRedirect())
    {
        //store the token in a session variable
        $_SESSION['facebook']=$session->getToken();
    }

    //If the session is already set
    if(isset($_SESSION['facebook']))
    {
        //New Facebook Session; Pass the facebook token
        $session= new \Facebook\FacebookSession($_SESSION['facebook']);

        //Creating a fb request, pass the session, method, /me to get the user's details
        $request= new \Facebook\FacebookRequest($session,'GET','/me');

        //Execute the request
        $request=$request->execute();

        $user=$request->getGraphObject()->asArray();

       // print_r($user);
    }

}
catch(Facebook\FacebookRequestException $e)
{
    //When FB returns an error
}
catch(\Exception $e)
{
    //Normal exception
}

#2


You are setting up a session, using an App Access token. That means that the access token only identifies your app; no users.

您正在使用App Access令牌设置会话。这意味着访问令牌只能识别您的应用;没有用户。

The call you are making, /me, is an alias for the current user ID of the used access token. But, there is no user ID for an app. And, with just an App Access token you can't request user information.

您正在进行的呼叫/我是使用的访问令牌的当前用户ID的别名。但是,应用程序没有用户ID。并且,仅使用App Access令牌,您无法请求用户信息。

To make your script work, you should query /1578373582426403, which is the App ID.

要使脚本正常工作,您应该查询/ 1578373582426403,这是应用程序ID。

Update: you can also query /app, which should be the alias for the app of an access token.

更新:您还可以查询/ app,它应该是访问令牌应用的别名。

#1


If you are trying to access the current user's information,try this. Change the redirect login

如果您尝试访问当前用户的信息,请尝试此操作。更改重定向登录

// Call Facebook Session- App Id, App Secret
Facebook\FacebookSession::setDefaultApplication('1578373582426403', 'CENSORED');

//Facebook Redirect Helper
//Create a FB variable, Pass oAuth URL
$facebook= new Facebook\FacebookRedirectLoginHelper('http://localhost/fb/');

//
try
{
    //To check method getSessionFromRedirect
    if($session= $facebook->getSessionFromRedirect())
    {
        //store the token in a session variable
        $_SESSION['facebook']=$session->getToken();
    }

    //If the session is already set
    if(isset($_SESSION['facebook']))
    {
        //New Facebook Session; Pass the facebook token
        $session= new \Facebook\FacebookSession($_SESSION['facebook']);

        //Creating a fb request, pass the session, method, /me to get the user's details
        $request= new \Facebook\FacebookRequest($session,'GET','/me');

        //Execute the request
        $request=$request->execute();

        $user=$request->getGraphObject()->asArray();

       // print_r($user);
    }

}
catch(Facebook\FacebookRequestException $e)
{
    //When FB returns an error
}
catch(\Exception $e)
{
    //Normal exception
}

#2


You are setting up a session, using an App Access token. That means that the access token only identifies your app; no users.

您正在使用App Access令牌设置会话。这意味着访问令牌只能识别您的应用;没有用户。

The call you are making, /me, is an alias for the current user ID of the used access token. But, there is no user ID for an app. And, with just an App Access token you can't request user information.

您正在进行的呼叫/我是使用的访问令牌的当前用户ID的别名。但是,应用程序没有用户ID。并且,仅使用App Access令牌,您无法请求用户信息。

To make your script work, you should query /1578373582426403, which is the App ID.

要使脚本正常工作,您应该查询/ 1578373582426403,这是应用程序ID。

Update: you can also query /app, which should be the alias for the app of an access token.

更新:您还可以查询/ app,它应该是访问令牌应用的别名。