阅读Facebook页面帖子和令牌到期 [英] Read Facebook page post and token expiration

查看:67
本文介绍了阅读Facebook页面帖子和令牌到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从服务器读取特定粉丝页面的流. 我试图阅读图形API https://graph.facebook.com//feed?access_token =& limit = 100 它的工作原理. 我需要了解的是令牌是否会过期以及如何以编程方式更新令牌. 现在,我通过 http://developers.facebook.com/tools/explorer/应用. 你能帮我么? 我正在使用PHP SDK 谢谢,

I need to read, from a server, the stream of a specific fan page. I tried to read the graph api https://graph.facebook.com//feed?access_token=&limit=100 and it works. What I need is to understand if the token will expire and how to renew it programmatically. Now I generate my token through the http://developers.facebook.com/tools/explorer/ app. Can you please help me? I'm using the PHP sdk thanks, a.

推荐答案

您可以使用以下代码阅读facebook页面,还可以获取指定的字段

You can read the facebook page using below codes and you can also get the specified fields

https://graph.facebook.com/$page_id/?fields=link,etc&access_token=page_access_token

$response = $fb->api($page_id .  '/?fields=link,etc&'. $access_token, 'GET')

以下是四种情况的解决方案

1.令牌在过期时间后过期(默认为2小时).
2.用户更改密码使访问令牌无效.
3.用户取消对您的应用的授权.
4.用户退出Facebook.

1.The token expires after expires time (2 hours is the default).
2.The user changes her password which invalidates the access token.
3.The user de-authorizes your app.
4.The user logs out of Facebook.

为确保为您的用户带来最佳体验,您的应用需要做好准备以捕获上述情况下的错误.以下PHP代码向您展示了如何处理这些错误以及如何检索新的访问令牌.

To ensure the best experience for your users, your app needs to be prepared to catch errors for the above scenarios. The following PHP code shows you how to handle these errors and retrieve a new access token.

当您将用户重定向到auth对话框时,如果用户已经授权了您的应用程序,则不会提示用户输入权限. Facebook将向您返回有效的访问令牌,而无需面对任何用户的对话框.但是,如果用户取消了对您的应用程序的授权,则该用户将需要重新授权您的应用程序,以使您获得access_token.

When you redirect the user to the auth dialog, the user is not prompted for permissions if the user has already authorized your application. Facebook will return you a valid access token without any user facing dialog. However if the user has de-authorized your application then the user will need to re-authorize your application for you to get the access_token.

<?php
$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_SSL_VERIFYPEER, false);
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;
}
?>

有关更多详细信息,您可以访问此链接
谢谢

for more details info you can visit this link
Thanks

这篇关于阅读Facebook页面帖子和令牌到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆