繁体   English   中英

Facebook PHP SDK - 如何获取访问令牌?

[英]Facebook PHP SDK - How to get access token?

我正试图从我的应用程序发布用户的Facebook墙。 用户授予应用程序的权限以在他的墙上发布,并且我在db中有userid。 我需要自动发送帖子,而无需用户再次登录。

我的代码是:

try{
    require_once(dirname(__FILE__) . '\lib\facebook-php\src\facebook.php' );
}
catch(Exception $o){
        print_r($o);
}
$config = array(
    'appId' => '123456',
    'secret' => '454544',
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';

if($user_id) {

    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        $ret_obj = $facebook->api('/me/feed', 'POST',
            array(
                'access_token' => $facebook->getAccessToken(),
                'link' => 'www.example.com',
                'message' => 'Posting with the PHP SDK!'
            ));
        echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

        // Give the user a logout link
        echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
    } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        var_dump($e);
        $login_url = $facebook->getLoginUrl( array(
            'scope' => 'publish_stream'
        ));
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {

    // No user, so print a link for the user to login
    // To post to a user's wall, we need publish_stream permission
    // We'll use the current URL as the redirect_uri, so we don't
    // need to specify it here.
    $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
    echo 'Please <a href="' . $login_url . '">login.</a>';

}

抛出An active access token must be used to query information about the current user. 错误并要求登录。 点击登录后,帖子会被发布。

如何在没有用户登录的情况下使用userid获取访问令牌?

当调用API端点/me时,活动访问令牌不可用时会发生这种情况

有两种解决方案可以解决这个问题

  • 使用userid而不是/ me

  • 在发送api呼叫之前设置访问令牌

首先,您可以拨打电话

ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
            array(
                'link' => 'www.example.com',
                'message' => 'Posting with the PHP SDK!'
            ));

你不需要发送'access_token' => $facebook->getAccessToken(),

第二个选择是做

$access_token =  $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
ret_obj = $facebook->api('/me/feed', 'POST',
                array(
                    'link' => 'www.example.com',
                    'message' => 'Posting with the PHP SDK!'
                ));

注意:必须在登录时发送publish_stream范围

取消通知: publish_stream被取代publish_actions ,检查Facebook的文档权限

你必须获得许可。 在这里查看

https://developers.facebook.com/docs/reference/login/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM