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>';
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀