Facebook PHP SDK访问令牌问题

时间:2015-09-01 08:21:15

标签: php facebook facebook-graph-api

我在使用Facebook Graph API处理令牌和没有重定向时遇到了一些问题。我通过URL获取令牌,但facebook只是无法识别它。这是代码:

$fb = new Facebook\Facebook([
    'app_id' => $details['app_id'],
    'app_secret' => $details['app_secret'],
    'default_graph_version' => $details['default_graph_version']
]);

$authToken = $this->helper->fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$details['app_id']}&client_secret={$details['app_secret']}");

$fb->setDefaultAccessToken($authToken);

$response = $fb->get($page.'/posts');

我得到的错误是FacebookResponseException:无效的OAuth访问令牌签名
在这一点上,我确定这不是正确的方法,但是我找不到任何关于正确方法的文档。

2 个答案:

答案 0 :(得分:2)

原来我试图用完整的返回字符串设置默认令牌,该字符串是 access_token = ACTUAL_TOKEN

$fb->setDefaultAccessToken($authToken);更改为

$info = explode('=', $authToken);
$fb->setDefaultAccessToken($info[1]);

解决了这个问题。

修改
更好的是,事实证明你不需要为这样的应用程序请求请求令牌,所以这也可以工作:

$fb = new Facebook\Facebook([
    'app_id' => $details['app_id'],
    'app_secret' => $details['app_secret'],
    'default_graph_version' => $details['default_graph_version'],
    'default_access_token' => $details['app_id'].'|'.$details['app_secret']
]);

$response = $fb->get($page.'/posts');

答案 1 :(得分:0)

for facebook api v4试试这个

我认为此回复https://github.com/codeNmind/facebook-php-sdk-v4可以帮助您     

use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookSignedRequestFromInputHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;

FacebookSession::setDefaultApplication('APP-ID','APP-SECRET-KEY');

$helper = new FacebookCanvasLoginHelper();

try {
    $session = $helper->getSession();
} catch (FacebookRequestException $ex) {
    echo $ex->getMessage();
} catch (\Exception $ex) {
    echo $ex->getMessage();
}

if ($session) {
    try {
        $request = new FacebookRequest($session, 'GET', '/me');
        $response = $request->execute();
        $me = $response->getGraphObject();
        echo $me->getProperty('name');
    } catch(FacebookRequestException $e) {
        echo $e->getMessage();
    }
} else {
    $helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/APP_NAMESPACE/'); // add your Facebook app namespace here

    // getting the login url for your app
    $auth_url = $helper->getLoginUrl(array('email')); // add new permission here as an index type array

    // redirecting the user directly to your Facebook canvas app you can also make a link of it
    echo "<script>window.top.location.href='".$auth_url."'</script>";
}