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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21887504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:30:31  来源:igfitidea点击:

Facebook PHP SDK - How to get access token?

phpfacebookfacebook-graph-api

提问by user1049961

I'm trying to post on user's Facebook wall from my app. The user granted the permissions to the app to post on his wall, and I have userid in db. I need to send the post automatically, without the user logging in again.

我正在尝试通过我的应用在用户的 Facebook 墙上发帖。用户授予应用程序在他的墙上张贴的权限,而我在 db 中有用户 ID。我需要自动发送帖子,而无需用户再次登录。

My code is:

我的代码是:

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>';

}

This throws An active access token must be used to query information about the current user.error and asks for login. After I click login, the post is sucesfully posted.

这会引发An active access token must be used to query information about the current user.错误并要求登录。单击登录后,该帖子已成功发布。

How can I get the access token with userid without the user logging in?

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

回答by Abhik Chakraborty

This happens when the active access token is not available while calling the API end point /me

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

There are two solutions to get rig of this

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

  • use the userid instead of /me

  • set the access-token prior sending the api call

  • 使用用户 ID 而不是 /me

  • 在发送 api 调用之前设置访问令牌

For the first you can make the call as

首先,您可以拨打电话

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

You dont need to send 'access_token' => $facebook->getAccessToken(),

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

The Second option is to do as

第二种选择是做

$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!'
                ));

NOTE: publish_streamscope must be send while doing the login

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

Deprecation Notice :publish_streamwas replaced by publish_actions, check the facebook doc for permission

弃用通知:publish_stream已被替换publish_actions,请查看 facebook 文档以获得许可

回答by Nishant Solanki

you have to get permission . Check it out here

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

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

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