开发者

Facebook Graph API getAccessToken() not working

开发者 https://www.devze.com 2023-03-26 05:23 出处:网络
I have been working on the Graph API for the past few days. When I try to make an API call using the PHP-SDK, i get an empty json object as a response. But when i copy the access token from the links

I have been working on the Graph API for the past few days. When I try to make an API call using the PHP-SDK, i get an empty json object as a response. But when i copy the access token from the links that are provided by FB in http://developers.facebook.com/docs/reference/api/, i get a json response.

I assume that this is because of the Access Token that i am sending in the request. Help Appreciated.

My Code :

include_once "fbInit.php";
$access = $facebook->getAccessToken();
$user = $facebook->getUser();
echo $access;
if(!$user)
{
$loginUrl = $facebook->getLoginUrl();
header("Location:".$loginUrl);
}
$userWall = $faceboo开发者_如何转开发k->api('/me/feed',array('access_token'=>$access))   ;
var_dump($userWall);


getAccessToken() only returns the internally stored access token in the SDK, it doesn't generate or fetch one. One advantage of using the SDK is that you don't need to pass in the access token directly like you're doing toward the end of your example. Once the user clicks the link generated by getLoginUrl(), the access token will automatically be stored in the SDK and the api method will work. Posting to a user's wall requires more parameters than you're passing in, however. You can see a complete example of how to do this in the recently-released PHP SDK docs at https://developers.intern.facebook.com/docs/reference/php/facebook-api/


This works for me:

index.php

<?php
    require_once("./config.php");
    $me = $facebook->api('/me');
    $access_token = $facebook->getAccessToken();
    $user = $facebook->getUser();
    $userWall = $facebook->api('/me/feed',array('access_token'=>$access_token));
    var_dump($userWall);
?>

config.php

<?php
    require_once("facebook.php");

    $app_id = "app_id";
    $app_secret = "app_secret";

    $facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
    ));

    if(is_null($facebook->getUser()))
    {
        header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
        exit;
    }
?>


The api suppose to fetch the access token for you - it form a access_token graph api at base_facebook.php line 664. Unfortunately this api call maybe failed due to your redirect uri - some had mentioned that you should not have a "?" in your redirect uri and in my case - there is a tail "/", which caused this call failed. And same redirect uri had no problem if you direct call through "https://graph.facebook.com/oauth/access_token?".

Try to put a log to print out the response result from facebook in _graph() function within base_facebook.php will help you quickly determine whether you are hit by same problem as me.

0

精彩评论

暂无评论...
验证码 换一张
取 消