在Facebook API中刷新令牌和访问令牌 [英] Refresh token and Access token in facebook API

查看:167
本文介绍了在Facebook API中刷新令牌和访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在google api上执行oauth2时,我们会获得访问令牌和刷新令牌。假设我正在写一个服务,我想定期轮询更改,我可以使用刷新令牌来获取新的访问令牌,每当当前访问令牌无效时。这被称为离线访问


$ b $在Facebook上有什么办法吗?是否有离线访问版本类似于google api。



谢谢。

解决方案

对于离线访问,您需要在新的访问令牌到期之前交换短命令访问令牌。 Facebook具有单一类型的访问令牌(不刷新令牌)。一个即将到期的访问令牌应该提取一个新的访问令牌。



使用Graph API端点手动扩展令牌::

  GET / oauth / access_token? 
grant_type = fb_exchange_token&
client_id = {app-id}&
client_secret = {app-secret}&
fb_exchange_token = {short-lived-token}

引用FB的文档从 here ::


应用程序无法兑换
长寿命令牌的过期短期令牌。上述流程仅适用于仍然有效的短期令牌
。一旦它们过期,您的应用程序必须通过登录流程再次发送用户


请阅读到期和扩展令牌我提到的文档链接的一部分进一步澄清。


When we do oauth2 on google api, we get an access token and a refresh token. Suppose I'm writing a service and I want to periodically poll for changes I can just use refresh token to get fresh access tokens every time the current access token gets invalidated. This is called offline access.

Is there any way to do the same in facebook? Is there an offline access version similar to that of google api.

Thanks.

解决方案

For offline access, you need to exchange your short-lived access token for a new access token, before it expires. Facebook has a single type of access token (no refresh tokens). A about-to-expire access token should fetch you a new access token.

To manually extend the tokens using a Graph API endpoint ::

GET /oauth/access_token?  
    grant_type=fb_exchange_token&           
    client_id={app-id}&
    client_secret={app-secret}&
    fb_exchange_token={short-lived-token}

Quoting FB's documentation from here ::

Apps are unable to exchange an expired short-lived token for a long-lived token. The flow above only works with short-lived tokens that are still valid. Once they expire, your app must send the user through the login flow again.

Do read the Expiration and Extending Tokens portion of the documentation link that I have mentioned for further clarification.

这篇关于在Facebook API中刷新令牌和访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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