Facebook PHP SDK 4.0:获取长期访问令牌 [英] Facebook PHP SDK 4.0 : Getting Long Term Access Token

查看:208
本文介绍了Facebook PHP SDK 4.0:获取长期访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP sdk v4.0获取用于PAGE管理的长期访问令牌.

I'm trying to use the PHP sdk v4.0 to get a long term access token for PAGE management.

我正在从用户的登录名中获取访问令牌(是的,我正在获取特定于页面的访问令牌).然后按照文档中的说明将其发送到端点,但是没有任何结果,也没有任何错误.

I'm grabbing the access token from the user's login (Yes, I'm grabbing the Page-specific access token). Then sending it to the endpoint as specified in documentation, but I'm not getting any results and I'm not getting any errors.

我知道要使用的正确代码段是什么吗?

Could I know what is the correct code snippet to use?

这是我到目前为止正在使用的代码

This is the code I'm using so far

$endpoint   =   '/oauth/access_token?';
$endpoint   .=  'grant_type=fb_exchange_token&';
$endpoint   .=  'client_id='.$this->app_id.'&';
$endpoint   .=  'client_secret='.$this->app_secret.'&';
$endpoint   .=  'fb_exchange_token='.$access_token;

try {

    $response = (new FacebookRequest(
        $this->session, 'GET', $endpoint
    ))->execute();

    // Do something with the response here but response is empty

} catch (FacebookRequestException $ex) {

    echo $ex->getMessage();

} catch (\Exception $ex) {

    echo $ex->getMessage();

}

推荐答案

有几个

There a several types of access tokens you can use with calls to Graph. Knowing which access token to use can be tricky.

如果您要更改页面并以admin用户身份发布到页面墙上,则需要使用该用户的访问令牌.

If you want to make changes to the page and post on the page wall as the admin user, you'll need to use that user's access token.

如果您打算在页面上执行特定于管理员的操作,则需要请该用户使用manage_pages权限登录.

You'll need to ask that user to log in with the manage_pages permission if you're planning on performing admin-specific actions on the page.

$helper = new FacebookRedirectLoginHelper($redirect_url);
echo '<a href="' . $helper->getLoginUrl(['manage_pages']) . '">Login</a>';

扩展用户访问令牌

默认情况下,您将从Facebook获得短暂的用户访问令牌.我假设您正在使用数据库来存储访问令牌.您需要将短期用户访问令牌交换为长期用户访问令牌,并将新令牌保存在数据库中.

Extending User Access Token

By default, you'll get a short-lived user access token from Facebook. I'm assuming you're using a database to store your access tokens. You'll need to exchange the short-lived user access token for a long-lived user access token and save the new token in the database.

$accessToken = $session->getAccessToken();
$longLivedAccessToken = $accessToken->extend();
echo (string) $longLivedAccessToken;

使用代码

如果要将长期用户访问令牌存储在数据库中,作为最佳实践,则应使用该令牌来生成code,然后再生成另一个长期访问令牌.这样,您就不会每次都代表用户对所有请求使用相同的访问令牌.这样可以最大程度地减少将您的应用标记为垃圾邮件的可能性.

Using a code

If you're storing the long-lived user access token in the database, as a best practice, you should use the token to generate a code and then generate another long-lived access token. This way you're not using the same access token for all the requests on behalf of the user every time. This minimizes the chances of your app being flagged as spam.

use Facebook\Entities\AccessToken;

$longLivedAccessToken = new AccessToken('{long-lived-access-token}');
$code = AccessToken::getCodeFromAccessToken($longLivedAccessToken);
$newLongLivedAccessToken = AccessToken::getAccessTokenFromCode($code);

页面访问令牌

如果您想在页面上张贴雕像并且让帖子看起来像页面已经发布了状态,则需要使用页面访问令牌.

Page Access Tokens

If you want to post statues on the page and have the posts appear as if the page had posted the statuses you'll need to use a page access token.

使用页面管理员的长期用户访问令牌,您可以在权限.

Using a page admin's long-lived user access token, you can list the pages that that user administrates on the /me/accounts endpoint. You'll want to pull the access_token field which is the page access token. You can also pull the perms field to see which permissions the admin user has.

$request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms');
$pageList = $request->execute()->getGraphObject()->asArray();

短期访问页面与长期访问页面令牌

如果您使用短暂的用户访问令牌来获取页面访问令牌,则页面访问令牌也将短暂存在.

Short-lived vs Long-lived page access tokens

If you use a short-lived user access token to obtain a page access token, the page access token will also be short-lived.

如果需要,可以直接将短期页面访问令牌与长期页面访问令牌交换.这将为您提供一个页面访问令牌,该令牌将持续大约2个月.

You could exchange the short-lived page access token with a long-lived page access token directly if you wanted to. This would give you a page access token that would last about 2 months.

$pageAccessToken = new AccessToken('{short-lived-page-access-token}');
$longLivedPageAccessToken = $pageAccessToken->extend();

但是,如果您使用长期的用户访问令牌来获取页面访问令牌,则页面访问令牌将永不过期.

However, if you use a long-lived user access token to obtain the page access token, the page access token will never expire.

您可以将页面访问令牌视为页面管理员用户访问令牌的子访问令牌".这是一个重要的概念,因为页面访问令牌与您从中获取页面访问令牌的管理员用户相关联.

You can think of page access tokens as "sub access tokens" to the page admin user access token. This is an important concept because page access tokens are associated with the admin user you obtained the page access token from.

由于页面管理员可以具有不同的页面管理员角色,因此如果未为管理员用户分配授予您所需的特定权限的角色,则会限制页面访问令牌的范围.

Since there are different page admin roles that a page admin can have, that will limit the scope of the page access token if the admin user isn't assigned the role that grants them a specific permission you need.

这篇关于Facebook PHP SDK 4.0:获取长期访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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