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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题