gpt4 book ai didi

php - 如何在不使用 api 资源管理器的情况下获取具有以前使用的权限的新 'short lived' facebook 访问 token (服务器端)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:45 24 4
gpt4 key购买 nike

简介


我有一个小的 Facebook 粉丝页面,我要在上面发布更新。为此,我使用了一个长期存在的访问 token ,该 token 每 60 天过期一次,其权限为:“manage_pages”和“publish_stream”。

经过研究,我没有找到“就是这样”的解决方案,所以我创建了一个新帖子来描述我的问题,同时将我目前发现的所有内容链接起来作为摘要。它长得有点大哈哈(我很抱歉)。

要获得短期访问 token ,我通常按照描述的这些步骤进行操作 here .

  • Go to https://developers.facebook.com/tools/explorer/ and select your app from the first drop down menu, in the left.
  • Click on the button "Get access token", and in the "Select Permissions" window, click in "Extended Permissions" and check manage_pages and publish_stream, and click in "Get Access Token" blue button.
  • You may be asked in this step to garant permissions to your app to access to your Facebook account, accept.
  • Next, click at the end of the text field next to the "GET" drop down, and replace the numbers for: me/accounts, and click in the blue button next to this text field.
  • You'll get the tokens for all your pages, including your app page. Find your page name in the list, will look like this: "name": "Your page name"
  • When you located your page, copy the access token for the page (will be really long), that can look like this: "access_token": "XXXXXXXX". Also copy the id of the page: "id": "XXXXX".

使用

https://graph.facebook.com/oauth/access_token?
client_id=[clientid]
&client_secret=[clientsecret]
&grant_type=fb_exchange_token
&fb_exchange_token=[shortlivedaccesstoken]

例如,无论是在浏览器中还是通过 curl 请求,我都会返回 60 天有效的访问 token 。 ( Source )

作为Facebook API documentation描述,长期存在的访问 token 无法刷新。当然,我可以使用上面的端点刷新 token 并获得一个新的长期 token 具有相同的到期日期返回。

编辑:要在粉丝页面墙上创建一个帖子(作为页面用户)我正在做某事。像这样:

$this->fb = new Facebook(array(
'appId' => [app-id],
'secret' => [app-secret]
));

//~Get data from DB here~

//~some error handling in case data fetching failed~


$fbToken = '';
$fbToken = *[facebook access token]*;

$mediaData = array(
'picture' => [picture-url],
'message' => [message],
'link' => [link],
'name' => [name],
'description' => [description],
'access_token' => $fbToken
);

try {
$post_id = $this->fb->api('/' . [fb-page-id] . '/feed', 'post', $mediaData);
} catch(FacebookApiException $e) {
//log file output
}



问题


您只能使用短期访问 token 来获取新的长期访问 token 。
@maxim-aniskov 描述了一种获取新的短期访问 token 的方法 here .遇到同样的问题,它在第一次获取时工作一次。接受的答案涉及删除当前权限。

此外,在 this post评论中已经暗示,如果没有用户交互,即使是短暂的访问 token 也无法刷新。

最后,正如您所看到的,有很多人遇到了同样的乐趣 here too .

我什至在某处读到可以刷新现有的长期访问 token ,但前提是它即将过期。没有关于“即将到期”的时间表的信息。 (5 天不是)

最终他们都没有进一步帮助我。


问题


综上所述~

How to get a new short lived access token with the previously used permissions (manage_pages and publish_stream) querying an API endpoint?

是否真的只能通过撤销先前授予的访问权限才能实现?我正在尝试刷新其访问 token 的网站是关于发布实时更新,所以我希望几乎没有停机时间.. 而不是每次旧 token 过期时在 graph api explorer 中手动生成一个新的短期 token 或即将到期,我宁愿只按一个按钮来完成。


最佳答案

其实你对流程很困惑!让我来解释一下这个访问 token 是如何工作的——

首先,从 Graph API Explorer 获取访问 token 只是一种方式——当然不是最好的方式。 (但在你的情况下,这似乎很好,因为我认为你只是从你的帐户管理页面,而不是为其他 Facebook 用户管理页面 - 如果我错了请纠正我)。

通常,facebook 集成用于获取 facebook 用户的访问权限并执行某些事件或获取用户的数据,这是通过实现 facebook login 来完成的。 . Facebook 登录考虑了用户授予的权限并返回 access token。 - 这是用户访问 token


现在,如果您想 代表页面本身发帖,您必须使用 页面的访问 token (API- /me/accounts ).现在,好处是 - 您可以扩展 永不过期的页面访问 token !我已经解释了这些步骤 here .

如果您想以用户/管理员的名义发帖,您可以使用用户访问 token ,它可以通过发出您已经解释过的\GET 请求来扩展。现在当你说-

I've even read that an existing long lived access token can be refreshed but only if it's about-to-expire. There is no information in which timeframe 'about-to-expire' lies. (5 days ain't)

这个说法是错误的!您可以随时扩展 token 。今天你有一个 60 天到期的代币,明天它可以再次延长,最多 60 天到期。要刷新扩展 token ,您必须重复第一次用于生成扩展 token 的步骤,即 - 使用登录流程或图形 api 资源管理器获取访问 token (正常的)(是的 - 没有其他方法获取访问 token )并发出\GET 请求以获取扩展 token 。就是这个!

你问-

How to get a new short lived access token with the previously used permissions (manage_pages and publish_stream) querying an API endpoint?

别担心,下次当您尝试获取访问 token 时,它将拥有相应的权限,因为您已经授权了该应用程序。你不需要为此处理任何事情!

编辑

如果您不想使用 Graph API Explorer,您可以使用 JS/PHP/其他语言编写一个小脚本并实现 facebook 登录 - 获取访问 token - 并扩展该 token (哦等等!你没有告诉你如何创建帖子?你只能用你的脚本来做到这一点 - 足够简单!)。祝你好运!

关于php - 如何在不使用 api 资源管理器的情况下获取具有以前使用的权限的新 'short lived' facebook 访问 token (服务器端)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22732843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com