doujing5846 2016-11-14 18:25
浏览 49
已采纳

Facebook扩展令牌仅适用于一页,如何休息

I have the following code to generate an extended token. No SDK involved. I get the initial temporary token with the Graph API Explorer.

  $api_action = 'me/accounts';
  $url = 'https://graph.facebook.com/' . $this->facebook_api_version . '/' . $api_action;
  $url_with_token = $url . '?access_token=' . $this->facebook_temporary_access_token . '';
  $account = json_decode(file_get_contents($url_with_token));
  $page_access_token = $account->data[0]->access_token;

  // Get extended access token
  $access_token = '';
  $url = 'https://graph.facebook.com/oauth/access_token?client_id=' . $this->facebook_app_id . '&client_secret=' . $this->facebook_app_secret . '&grant_type=fb_exchange_token&fb_exchange_token=' . $page_access_token;
  $result = file_get_contents($url);
  $result = str_replace('access_token=', '', $result);
  $result = explode('&expires', $result);
  $facebook_extended_access_token = $result[0];
  echo '<pre>'; print_r($facebook_extended_access_token); echo '</pre>';
  return $facebook_extended_access_token;

This application has three pages associated with it. The problem is this only generates an extended token for the Page A. I've verified using the Facebook Toke Debugger.

https://developers.facebook.com/tools/debug/access_token?q=

How can I generate an extended token for Facebook Page B and C and not just A.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-11-15 14:14
    关注

    Credit to CBroe's comment.

    I found that the first API call in my code was actually returning temporary access tokens for all of my pages, but I was set up to only return the first. $account->data[1]->access_token and $account->data[2]->access_token hold B and C. Here is my final code for those that find it useful.

      $api_action = 'me/accounts';
      $url = 'https://graph.facebook.com/' . $this->facebook_api_version . '/' . $api_action;
      $url_with_token = $url . '?access_token=' . $this->facebook_temporary_access_token . '';
      $account = json_decode(file_get_contents($url_with_token));
    
      // Loop through each store
      foreach ($account->data as $data) {
        $page_access_token = $data->access_token;
        // Get extended access token
        $access_token = '';
        $url = 'https://graph.facebook.com/oauth/access_token?client_id=' . $this->facebook_app_id . '&client_secret=' . $this->facebook_app_secret . '&grant_type=fb_exchange_token&fb_exchange_token=' . $page_access_token;
        $result = file_get_contents($url);
        $result = str_replace('access_token=', '', $result);
        $result = explode('&expires', $result);
        $facebook_extended_access_token = $result[0];
        echo '<pre>'; print_r($facebook_extended_access_token); echo '</pre>';
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度