如何获取访问令牌

如何获取访问令牌

本文介绍了Facebook PHP SDK - 如何获取访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的应用程序发布在用户的Facebook墙上。用户授予应用程序的权限在他的墙上发布,我在db中有userid。我需要自动发送邮件,而不用户再次登录。



我的代码是:

  try {
require_once(dirname(__ FILE__)。'\lib\facebook-php\src\facebook.php');
}
catch(异常$ o){
print_r($ o);
}
$ config = array(
'appId'=>'123456',
'secret'=>'454544',
'allowSignedRequest'= > false //可选,但应为非画布应用程序设置为false
);

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

if($ user_id){

//我们有一个用户ID,所以可能是一个登录用户。
//如果没有,我们将得到一个异常,我们在下面处理。
尝试{

$ ret_obj = $ facebook-> api('/ me / feed','POST',
数组(
'access_token'=> ; $ facebook-> getAccessToken(),
'link'=>'www.example.com',
'message'=>'发布PHP SDK!'
));
echo'< pre>帖子ID:'。 $ ret_obj ['id']。 < /预>;

//给用户注销链接
echo'< br />< a href ='。$ facebook-> getLogoutUrl()。'> logout< ; / A>';
} catch(FacebookApiException $ e){
//如果用户注销,即使访问令牌无效,也可以使用
//用户ID。
//在这种情况下,我们会得到一个异常,所以我们要
//只是要求用户再次登录。
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 {

//没有用户,所以打印一个链接供用户登录
//要发布到用户的墙上,我们需要publish_stream权限
//我们将使用当前的URL作为redirect_uri,所以我们不需要在这里指定
//。
$ login_url = $ facebook-> getLoginUrl(array('scope'=>'publish_stream'));
echo'Please< a href ='。$ login_url。'> login。< / a>';

}

这会抛出一个活动的访问令牌必须用于查询有关当前用户的信息。错误并要求登录。点击登录后,该帖子被强制发布。



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

解决方案

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



有两个解决方案来获取这个




  • 使用userid而不是/ me


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




对于第一个您可以将该电话作为

  ret_obj = $ facebook-> api('/'.$ user_id。'/ feed' ,'POST',
数组(
'link'=>'www.example.com',
'message'=>'使用PHP SDK发布!'
));

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



第二个选项是作为

  $ access_token = $ facebook-> getAccessToken(); 
$ facebook-> setAccessToken($ access_token);
ret_obj = $ facebook-> api('/ me / feed','POST',
数组(
'link'=>'www.example.com',
'message'=>'使用PHP SDK发布!'
));

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



弃用通知: publish_stream 被替换为 publish_actions ,请查看Facebook文档以获取


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.

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.

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

解决方案

This happens when the active access token is not available while calling the API end point /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

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(),

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_stream scope must be send while doing the login

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

这篇关于Facebook PHP SDK - 如何获取访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:05