php - facebook graph api 从 2.2 到 2.3 不工作

标签 php facebook facebook-graph-api

因为它是 graph api 2.2 的截止日期,我正在尝试使用 v2.3 修复我的 graph api 但是当我使用 2.3 时,我发现大多数 api 请求响应都没有,但我在升级文档中找不到任何更新。例如:

https://graph.facebook.com/v2.3/{$user_id}?date_format=U&fields=albums.order(reverse_chronological).limit(100).offset(0){id,count,name,created_time}

如果我使用 2.3,将不会返回任何内容。 我打电话时无法获得用户的生日:

https://graph.facebook.com/v2.3/{$user_id}

它只是返回名称和居住位置。 但在 v2.2 中,它包括生日配置文件。

我使用 facebook SDK 3.2.2 因为我的 php 版本是 5.3。 有什么我不知道的更新吗?谢谢。

最佳答案

我自己发现了问题。这是因为 SDK 3.2.2。对于 facebook 更新(来自 API 版本 2.3 的 Changelog):

[Oauth Access Token] Format - The response format of https://www.facebook.com/v2.3/oauth/access_token returned when you exchange a code for an access_token now return valid JSON instead of being URL encoded. The new format of this response is {"access_token": {TOKEN}, "token_type":{TYPE}, "expires_in":{TIME}}. We made this update to be compliant with section 5.1 of RFC 6749.

但 SDK 将响应识别为数组(在 getAccessTokenFromCode 函数中):

$response_params = array();
parse_str($access_token_response, $response_params);
if (!isset($response_params['access_token'])) {
  return false;
}
return $response_params['access_token'];

这将无法正确获取用户访问 token ,并且您无法获取用户的数据。因此,您应该更新此函数以将数据解析为 json:

$response = json_decode($access_token_response);
if (!isset($response->access_token)) {
  return false;
}
return $response->access_token;

然后所有功能将照常工作。


此外,您必须对 setExtendedAccessToken() 进行类似的更改。否则,您的应用将无法扩展访问 token 。下面的代码演示了如何升级该功能。

  /**
   * Extend an access token, while removing the short-lived token that might
   * have been generated via client-side flow. Thanks to http://bit.ly/ b0Pt0H
   * for the workaround.
   */
  public function setExtendedAccessToken() {
    try {
      // need to circumvent json_decode by calling _oauthRequest
      // directly, since response isn't JSON format.
      $access_token_response = $this->_oauthRequest(
        $this->getUrl('graph', '/oauth/access_token'),
        $params = array(
          'client_id' => $this->getAppId(),
          'client_secret' => $this->getAppSecret(),
          'grant_type' => 'fb_exchange_token',
          'fb_exchange_token' => $this->getAccessToken(),
        )
      );
    }
    catch (FacebookApiException $e) {
      // most likely that user very recently revoked authorization.
      // In any event, we don't have an access token, so say so.
      return false;
    }

    if (empty($access_token_response)) {
      return false;
    }

    //Version 2.2 and down (Deprecated).  For more info, see http://stackoverflow.com/a/43016312/114558
    // $response_params = array();
    // parse_str($access_token_response, $response_params);
    //
    // if (!isset($response_params['access_token'])) {
    //   return false;
    // }
    //
    // $this->destroySession();
    //
    // $this->setPersistentData(
    //   'access_token', $response_params['access_token']
    // );

    //Version 2.3 and up.
    $response = json_decode($access_token_response);
    if (!isset($response->access_token)) {
      return false;
    }

    $this->destroySession();

    $this->setPersistentData(
      'access_token', $response->access_token
    );
  }

关于php - facebook graph api 从 2.2 到 2.3 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42994019/

相关文章:

Facebook 应用程序 : Additional permissions

php - 使用ajax jquery运行php脚本

javascript - FB api - 在其他人的公共(public)页面上发布图像

php - Zend_工具 : Fatal error: Cannot redeclare class Zend_Loader

ios - 阅读非好友的动态消息

python - 我应该合并内存 (.py) 中的图像还是 View (HTML) 中的图像?

python - 如何在 GAE Python 中使用 Facebook Graph API 获取用户电子邮件?

javascript - 为什么 Google Apps 脚本的 UrlFetchApp 返回 HTML 而不是 csv 文件(请求 Facebook API)?

php - Android Volley 库

php - 修复 SQL 和循环以自动将数据放入 CGridView