有没有办法检查Facebook访问令牌是否仍然有效?

我的网站使用终身访问令牌( offline_access )。 但是,如果用户更改其密码,访问令牌将被重置。 有没有一种方法来调用graphicsAPI之前检查当前访问令牌是否有效? 谢谢你的时间。

基本上,FB要你轮询它,或者检测情况,并redirect用户获得一个真相发生。 讨厌,但官方:

(旧的,过时的链接,见下文) https://developers.facebook.com/blog/post/500/

编辑:Facebook改变了他们的链接结构没有redirect。 并不意外。

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

离线,没有发送任何东西到Facebook – 我不这么认为。 最简单的方法可能是发送请求到:

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

Facebook还支持订阅实时更新,但我不知道如何将其应用于这种情况。

如果你想知道令牌到期时间,你可以使用appid和令牌传递一个开放graphicsurl,如下所示。

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

实时更新可以让你解决这个问题,但这将是非常复杂的。 基本上,你可以订阅更新,告诉你1)用户是否删除了应用程序或2)如果用户删除了权限。 您可以使用它来存储Faceboook用户的当前权限。 这样,如果用户移除了您的应用程序,您将知道访问令牌已过期。

实时更新实际上是Facebook推荐的处理权限的方式。 每次加载页面时,许多应用都会进行api调用以检查权限。 这往往是缓慢和不可靠的。

  //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; } 

离线 – 这是不可能的

询问用户是否允许:

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

如果访问令牌是无效的,那么它会给出错误:

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

否则,会给出用户给出的权限列表:

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

从OP开始更新它,

您可以在这里debugging访问令牌: https : //developers.facebook.com/tools/debug/accesstoken? version = v2.5 & q = {access_token}

奥托的脸书post的答案似乎是对这个问题的官方回应,但它使用直接的PHP而不是SDK,也使用JS来解决问题,而不是PHP。 如果您使用PHP来检查有效的会话,则通常需要一个确保有效会话的PHP方法才能继续。

下面的代码用graphicsAPI检查我的对象。 如果抛出exception,会破坏当前的Facebook会话。

 try{ $facebook->api('/me'); } catch( FacebookApiException $e ){ $facebook->destroySession(); } 

这迫使稍后的graphics调用来实例化新的Facebook会话。 这至less可以让你访问公共数据,这样就可以渲染页面不需要FB用户的权限:

 $facebook->api('/userName'); 

要重新获得用户权限,用户将需要login到您的应用程序(这与login到Facebook本身不同)。 你可以用JS或PHP来做到这一点:

 $facebook->getLoginUrl(); 

*请注意,destroySession()调用不在PHP SDK的标签版本中。 使用主分支或修补它。