web-dev-qa-db-ja.com

Facebookアクセストークンがまだ有効かどうかを確認する方法はありますか?

私のサイトはライフタイムアクセストークン(offline_access)。ただし、ユーザーがパスワードを変更すると、アクセストークンがリセットされます。 Graph APIを呼び出す前に、現在のアクセストークンが有効かどうかを確認する方法はありますか?御時間ありがとうございます。

71
axsuul

基本的に、FBはそれをポーリングするか、ケースを検出してユーザーをリダイレクトし、再認証を発生させます。迷惑ですが、公式:

(古い、古いリンク。下記参照)https://developers.facebook.com/blog/post/500/

編集:Facebookはリダイレクトなしでリンク構造を変更しました。驚きません。

https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/

37
Otto

Facebookに何も送信せずにオフラインで-私はそうは思いません。最も簡単な方法は、おそらく次のものにリクエストを送信することです。

https://graph.facebook.com/me?access_token=...

Facebookは リアルタイム アップデートのサブスクリプションもサポートしていますが、この状況にどのように適用するのかわかりません。

69
serg

トークンの有効期限を知りたい場合は、以下のようにappidとtokenを使用して開いているグラフのURLを渡すことができます。

https://graph.facebook.com/oauth/access_token_info?client_id=APPID&access_token=xxxxxxxxx
43

トークンデバッグサービスを使用してトークンを確認できます。こちらをご覧ください。

https://graph.facebook.com/debug_token?input_token=INPUT_TOKEN&access_token=ACCESS_TOKEN

https://developers.facebook.com/docs/howtos/login/debugging-access-tokens/

7
Avi Kapuya

リアルタイムの更新により、この問題を解決できますが、かなり複雑です。基本的に、1)ユーザーがアプリを削除した場合、または2)ユーザーがアクセス許可を削除した場合に通知する更新を購読できます。これを使用して、faceboookユーザーの現在の権限を保存できます。これにより、ユーザーがアプリを削除した場合、アクセストークンの有効期限が切れていることがわかります。

リアルタイム更新は、実際にはFacebookが推奨するアクセス許可の処理方法です。多くのアプリは、アクセス許可を確認するためにページが読み込まれるたびにAPI呼び出しを行います。これは遅く、信頼性が低い傾向があります。

4
Nathan Totten
        //When user access token expires user must be logged in and renew the access token him self.it is a Facebook policy 
        //you can overcome this by sending email to users who have expired access token.
        //create a table of successful sending to monitor sending process
        //if any failure happened with the user an email is sent to him to ask him to activate there account again.with a link to your subscription page.
        //and here is the code should be written on that page. 
         $app_id = "YOUR_APP_ID";
         $app_secret = "YOUR_APP_SECRET"; 
         $my_url = "YOUR_POST_LOGIN_URL";

        // known valid access token stored in a database 
        $access_token = "YOUR_STORED_ACCESS_TOKEN";

        $code = $_REQUEST["code"];

       // If we get a code, it means that we have re-authed the user 
       //and can get a valid access_token. 
       if (isset($code)) {
         $token_url="https://graph.facebook.com/oauth/access_token?client_id="
           . $app_id . "&redirect_uri=" . urlencode($my_url) 
           . "&client_secret=" . $app_secret 
           . "&code=" . $code . "&display=popup";
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $access_token = $params['access_token'];
       }


       // Attempt to query the graph:
       $graph_url = "https://graph.facebook.com/me?"
         . "access_token=" . $access_token;
       $response = curl_get_file_contents($graph_url);
       $decoded_response = json_decode($response);

       //Check for errors 
       if ($decoded_response->error) {
       // check to see if this is an oAuth error:
         if ($decoded_response->error->type== "OAuthException") {
           // Retrieving a valid access token. 
           $dialog_url= "https://www.facebook.com/dialog/oauth?"
             . "client_id=" . $app_id 
             . "&redirect_uri=" . urlencode($my_url);
           echo("<script> top.location.href='" . $dialog_url 
          . "'</script>");
        }
        else {
          echo "other error has happened";
        }
      } 
      else {
      // success
        echo("success" . $decoded_response->name);
        echo($access_token);
      }

      // note this wrapper function exists in order to circumvent PHP's 
      //strict obeying of HTTP error codes.  In this case, Facebook 
      //returns error code 400 which PHP obeys and wipes out 
      //the response.
      function curl_get_file_contents($URL) {
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
        curl_close($c);
        if ($contents) return $contents;
        else return FALSE;
      }
1
ashraf mohammed

オフライン-それは不可能です

ユーザーに許可を与えたかどうかを尋ねます。

https://graph.facebook.com/{facebook-id}/permissions?access_token={access-token}

アクセストークンが無効な場合、エラーが発生します。

{  
   error:{  
      message:"The access token could not be decrypted",
      type:"OAuthException",
      code:190
   }
}

それ以外の場合は、ユーザーが付与した権限のリストを提供します。

data:[  
   {  
      installed:1,
      ...... permission list......... 
      bookmarked:1
   }
]
1
Hitesh Modha

私はこれらの投稿を調べましたが、このような非常に良い解決策を見つけました:

GET graph.facebook.com/debug_token?
    input_token={token-to-inspect}
    &access_token={app_id}|{app_secret}

このリクエストからの応答により、必要なものすべてが提供されます。

  • アプリID-これは、トークンがアプリケーションからのものであることを検証します
  • アプリケーション名-これもチェック可能
  • expires_at-トークンの有効期限
  • is_valid-チェックのためのブール値
  • ser_id-比較および確認することもできます

「|」サインは手紙としてそこになければなりません

1
Pavol Golias

OP以降の状況が変わったため、これを更新します。

ここでアクセストークンをデバッグできます: https://developers.facebook.com/tools/debug/accesstoken?version=v2.5&q= {access_token}

0
Kevin Cantwell