dongzhuo3202 2013-03-23 13:39
浏览 235
已采纳

Facebook API:/ oauth / access_token无效

I am trying to obtain a user access token from Facebook with the PHP-SDK. The server-side log-in process works great, redirecting to the correct page and providing $code too.

After the redirect (and checking whether $code exists) I then call the following API method to exchange $code for a user token (API parameters are suppressed here):

$fb->api('/oauth/access_token',array(
    'client_id'     => APP_ID,
    'client_secret' => APP_SECRET,
    'redirect_uri'  => REDIRECT_URI,
    'code'          => $code
));

$fb_token = $fb->getAccessToken();
echo $fb_token;

Unfortunately, all I get from this script is still the app token. In fact, reducing the script to just the following does not make any difference:

$fb_token_user = $fb->getAccessToken();
echo $fb_token_user;

So I assume it is all about the /oauth/access_token call which for some reason is not working or at least does no action. It does not return any errors either.

I have tried and tested several alternatives, such as the get_file_contents() method suggested in the Facebook tutorial as well as adding additional parameters to the above API call (e.g. type=client_cred and more), but never succeeding.

Does anybody see why the API call is not working? Is it the script or could it be some app setting on Facebook?

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dongnaota6386 2013-03-24 10:11
    关注

    Solved! The code in the question is still not working in spite of ever so many threads on this and other communities stating it does (oddly enough it seemed to work for them).

    So I figured I had to go back to the file_get_contents() method as explained by Facebook and it turns out this method was previously not working because allow_url_fopen is disabled on my server. Not wanting to undergo the risk of enabling it, I discovered cURL as a work-around.

    And here is the working code:

    // cURL Load Function
    function cURLget ($ch_url) {
      $ch = curl_init();
      curl_setopt($ch,CURLOPT_URL,$ch_url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
      curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
      $ch_send = curl_exec($ch);
      curl_close($ch);
      return $ch_send;
    };
    
    // Obtain User Token
    $fb_token_get = cURLget("https://graph.facebook.com/oauth/access_token"
      . "?client_id=" . APP_ID
      . "&client_secret=". APP_SECRET
      . "&redirect_uri=" . urlencode(REDIRECT_URI)
      . "&code=" . $CODE
    );
    $fb_token_params = null;
    parse_str($fb_token_get,$fb_token_params);
    
    // Print Token Data
    echo "Token: " . $fb_token_params['access_token']
      . "<br />Expires: " . $fb_token_params['expires'];
    

    Thanks to @FabioAntunes for trying to help me with the original code and to @ShawnECarter for his script for the cURL Load Function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题