如何发布在Facebook图形api中使用php sdk将用户添加到角色

时间:2014-10-26 06:17:47

标签: facebook-graph-api facebook-php-sdk

我想使用php-sdk为图表api创建一个用户作为我的应用程序的管理员。 这是facebook在docs中提供的代码 -

/* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'POST',
'/{app-id}/roles',
array (
'user' => '123456789',
'role' => 'administrators',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */

编辑:
我浏览了这段代码,但它在文档中说,"需要任何应用管理员的用户访问令牌。"。 我没有进一步,如何做到这一点。

<?php
    session_start();
    require_once('Facebook/FacebookSession.php');
    require_once('Facebook/FacebookRedirectLoginHelper.php');
    require_once('Facebook/FacebookRequest.php');
    require_once('Facebook/FacebookResponse.php');
    require_once('Facebook/FacebookSDKException.php');
    require_once('Facebook/FacebookRequestException.php');
    require_once('Facebook/FacebookAuthorizationException.php');
    require_once('Facebook/GraphObject.php');
    require_once('Facebook/GraphUser.php');
    require_once('Facebook/GraphSessionInfo.php');
    require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
    require_once( 'Facebook/HttpClients/FacebookCurl.php' );
    require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
    require_once( 'Facebook/Entities/AccessToken.php' );
    require_once( 'Facebook/Entities/SignedRequest.php' );

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;

    // init app with app id (APPID) and secret (SECRET)
    FacebookSession::setDefaultApplication('my-app-id','my-app-secret');
    $helper = new FacebookRedirectLoginHelper( 'Redirect URL' );

    try 
    {
      $session = $helper->getSessionFromRedirect();
    } 
    catch( Exception $ex ) 
    {
      // When validation fails or other local issues
    }

    // see if we have a session
    if(isset($session))
    {
        $_SESSION['token'] = $session->getToken();
    $token='Hard Coded access_token of admin of app';

        echo "Logged in successfully.";
        $user_id = $session->getSessionInfo()->asArray()['user_id'];
        $request = new FacebookRequest($session, 'GET', '/App-ID/roles?access_token='.$token,array('user' => $user_id,'role' => 'administrators'));
        $response = $request->execute();
        $graph = $response->getGraphObject(GraphUser::className());
        echo "<br>Hi ". $graph->getId()."<br>";
    } 
    else 
    {
      // show login url
      $params = array(
      'scope' => 'user_friends,email,user_photos,offline_access');

      echo '<a href="' . $helper->getLoginUrl($params) . '">Login</a>';
    }
    ?>
提供了

access_token,但现在它提供了错误,因为&#34; API参数&#34;中提供的appsecret_proof无效。 我也在应用程序中启用了appsecret_proof,并在应用程序中将其传递给了

    $appsecret_proof= hash_hmac('sha256', $token,'App-Secret'); 
    $request = new FacebookRequest($session, 'GET', '/App-ID/roles?access_token='.$token.'&appsecret_proof='.$appsecret_proof,array('user' => $user_id,'role' => 'administrators'));

1 个答案:

答案 0 :(得分:1)

您需要为请求添加用户访问令牌。对用户进行身份验证时,您应该能够为用户检索访问令牌。您尚未提供登录流程,因此我无法查看您是否可以检索此内容。

无论如何,一旦您获得了用户访问令牌,只需将其附加到请求中,如下所示:

$request=new FacebookRequest($session,'POST','/{app-id}/roles?access_token={access_token}',array('user'=>'123456789','role'=>'administrators')); 

$response=$request->execute(); 

$graphObject=$response->getGraphObject(); 

文档在,之后的请求中显示幻像'role'->'administrators'。它不是必需的我不相信(并且可能会导致错误,尽管您的请求似乎产生了响应)。

https://developers.facebook.com/docs/graph-api/reference/v2.1/app/roles