[英]FACEBOOK API : How do I get the long-lived access token from PHP API
My question: how to get long-lived access token from PHP API.我的问题:如何从 PHP API 获取长期访问令牌。 I have read through all the prior posts about this matter and still don't have the answer.我已经阅读了之前所有关于此事的帖子,但仍然没有答案。 Basically, I get a short-lived access token from the API explorer.基本上,我从 API 资源管理器中获得了一个短期访问令牌。 Subsquently, I wrote a simple PHP prohgram invoking the facebook graph API to request a long-lived access token.随后,我编写了一个简单的 PHP 程序,调用 facebook 图形 API 来请求一个长期存在的访问令牌。 Yet, it isn't working.然而,它不起作用。 I am missing something here.我在这里遗漏了一些东西。 Here is the snippet of the PHP code that I wrote:这是我编写的 PHP 代码片段:
$response = $fb->request("GET", "GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={my app id}&
client_secret={my app secret}&
fb_exchange_token={'the short-lived token obtained from the api
explorer'}")
..//error checking...
//
// then I call this:
$token = $response->getaccessToken();
// end of program
It turns out the getaccessToken() return the same short-lived token that I passed to the API.事实证明 getaccessToken() 返回与我传递给 API 相同的短期令牌。 So, I am running of idea of how to make it work.所以,我正在思考如何让它发挥作用。
According to the API doc, the call with the input parameter of "fb_exhcnage_token" is supposed to return a "long-lived access token".根据 API 文档,输入参数为“fb_exhcnage_token”的调用应该返回“长期访问令牌”。 Yet, I am not getting it.然而,我不明白。
This is valid in 2019, after long-term tokens became 60-day expiries.这在 2019 年有效,在长期代币到期后 60 天。 Here are Facebook's instructions to swap a short-term token (provided in front-end) for a long-term token (server only):以下是 Facebook 将短期令牌(在前端提供)换成长期令牌(仅限服务器)的说明:
https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing/ https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing/
Generate a Long-lived User or Page Access Token
You will need the following:
A valid User or Page Access Token
Your App ID
Your App Secret
Query the GET oath/access_token endpoint.
curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?
grant_type=fb_exchange_token
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={your-access-token}"
Sample Response
{
"access_token":"{long-lived-access-token}",
"token_type": "bearer",
"expires_in": 5183944 //The number of seconds until the token expires
}
You can make this curl request in PHP using curl_init()
:您可以使用curl_init()
在 PHP 中发出此 curl 请求:
https://www.php.net/manual/en/book.curl.php https://www.php.net/manual/en/book.curl.php
Or if you would rather use the Facebook PHP SDK, you can do this:或者,如果您更愿意使用 Facebook PHP SDK,您可以这样做:
1.) Create a FB request object with your credentials. 1.) 使用您的凭据创建一个 FB 请求对象。 2.) Request long-term token using the FB object and the access token 2.) 使用 FB 对象和访问令牌请求长期令牌
public function createFacebookRequestObject($facebookUser)
{
try {
return new Facebook([
'app_id' => config('env.FACEBOOK_GRAPH_API_ID'),
'app_secret' => config('env.FACEBOOK_GRAPH_API_SECRET'),
'default_graph_version' => 'v4.0',
'default_access_token' => $facebookUser->access_token
]);
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
return 'Facebook SDK returned an error: ' . $e->getMessage();
}
}
public function fetchLongTermAccessToken($fb, $facebookUser)
{
return $fb->get(
"/oauth/access_token?grant_type=
fb_exchange_token&fb_exchange_token="
. $facebookUser['access_token']
. '&client_id='
. config('env.FACEBOOK_APP_ID'), );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.