简体   繁体   English

FACEBOOK API:如何从 PHP API 获取长期访问令牌

[英]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.

相关问题 Facebook PHP SDK-如何获取长期存在的页面访问令牌 - Facebook PHP SDK - how to get a long lived page access token Facebook的长期访问令牌可以在多个用户上使用吗? - Is Facebook long-lived access token usable on multiple users? Facebook PHP SDK长期访问令牌在刷新站点后过期 - Facebook PHP SDK Long-lived access token expires after refresh site 刷新 Instagram 长期访问令牌适用于浏览器 (http) 但不适用于 PHP (curl) - Refreshing Instagram long-lived access token works in browser (http) but not PHP (curl) Facebook长期访问令牌 - Facebook Long lived access token 如何在不使用api资源管理器的情况下使用以前使用的权限获取新的“短期”facebook访问令牌(服务器端)? - How to get a new 'short lived' facebook access token (serverside) with the previously used permissions while not using the api explorer? 我如何才能使用Facebook php SDK获得长期支持 - How can I get a long lived accestoken with facebook php SDK 使用setExtendedAccessToken()获取长期访问令牌将返回短期令牌 - Getting long-lived access token with setExtendedAccessToken() returns short lived token 如何获得Graph Api For PHP facebook SDK的万岁访问令牌? - How to get Long live access token for Graph Api For PHP facebook SDK? 扩展长期访问令牌 - Extending Long-lived access tokens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM