好吧,您可以使用扩展用户访问令牌使其最长工作 60 天。之后,令牌将过期,然后您必须刷新令牌,而这在没有用户交互的情况下是不可能的。
文档声明相同:
即使是长期存在的访问令牌最终也会过期。在任何时候,您都可以通过将人员发送回您的 Web 应用程序使用的登录流程来生成新的长期令牌 - 请注意,此人实际上不需要再次登录,他们已经授权您的应用程序,因此他们将立即使用刷新的令牌从登录流程重定向回您的应用程序 - 人们对此的显示方式将根据您使用的登录流程类型而有所不同,例如,如果您使用的是 JavaScript SDK,这将发生在在后台,如果您使用的是服务器端流程,浏览器将快速重定向到登录对话框,然后自动立即再次返回您的应用程序。
完成上述操作后,您将获得一个新的短期令牌,然后您需要执行与上述相同的长期令牌交换。
要获取扩展令牌,请从您的服务器进行以下调用:
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
代码
$app_id = APP_ID;
$app_secret = APP_SECRET;
$post_login_url = APP_URL;
$album_name = 'test';
$album_description = 'desc';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
// access token
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token']; // <---
// extended token
$extended_token_url= "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=".$app_id."&client_secret=".$app_secret."&fb_exchange_token=".$access_token;
$response = file_get_contents($extended_token_url);
$params = null;
parse_str($response, $params);
$extended_access_token = $params['access_token']; // <---
// Upload to the photos to the album here
}
这就是完整的身份验证过程,包括扩展令牌。您可以找到许多有关照片上传的帖子,只需在$extended_access_token
您的电话中使用。
(如果您不知道,Graph API Explorer是 facebook 测试 API 的绝佳工具)。
祝你好运!