4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HybridAuth(Facebook PHP SDK v3.2.2)でFacebookログインができなくなる問題に対処

4
Last updated at Posted at 2017-03-29

課題

3/28より、FacebookのID連携をしているサービスで急にログインができなくなった。
ID連携には、HybridAuthというPHPのOAuthのライブラリを使っている。

原因

Facebook API ver2.2まではアクセストークンがクエリ文字列で access_token=xxx みたいに返ってきていた。
3/28のAPIアップデートにより、ver2.2が廃止され、ver2.3が最低バージョンになった。ver2.3以降は、戻り値がクエリ文字列ではなく、JSONになったのが原因。アクセストークンを取り出せずエラーになってしまう。
(参考) https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow

response.json
{
  "access_token": {access-token}, 
  "token_type": {type},
  "expires_in":  {seconds-til-expiration}
}

対処

APIバージョンを指定

エンドポイントの最後に /v2.8 のようにバージョンを指定。
これをしないと、呼び出しでバージョンを指定しないと、利用できる最も古いバージョンにデフォルトが設定されるということで、予期しない動作をする可能性があるので。

base_facebook.php
  /**
   * Maps aliases to Facebook domains.
   */
  public static $DOMAIN_MAP = array(
    'api'         => 'https://api.facebook.com/v2.8/',
    'api_video'   => 'https://api-video.facebook.com/v2.8/',
    'api_read'    => 'https://api-read.facebook.com/v2.8/',
    'graph'       => 'https://graph.facebook.com/v2.8/',
    'graph_video' => 'https://graph-video.facebook.com/v2.8/',
    'www'         => 'https://www.facebook.com/v2.8/',
  );

アクセストークンをJSONとしてパース

Facebook SDKの、getAccessTokenFromCode という関数の中身で、レスポンスをJSONでパースするように変更

(変更前)base_facebook.php
    $response_params = array();
    parse_str($access_token_response, $response_params);
    if (!isset($response_params['access_token'])) {
      return false;
    }

    return $response_params['access_token'];

(変更後)base_facebook.php
    $response_params = array();
    $response_params = json_decode($access_token_response);
    if (!isset($response_params->access_token)) {
      return false;
    }

    return $response_params->access_token;

参考

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?