且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中获取用户访问令牌

更新时间:2022-10-23 13:14:37

无法直接获取用户访问令牌登录用户与您的应用程序使用facebook身份验证流程进行交互。
没有用户可以获得的唯一令牌是应用程序令牌,您已经覆盖了。



您可以手动创建用户令牌,我认为最简单的方法是使用图形浏览器工具
只需选择右上角的应用程序,然后单击获取访问令牌按钮,然后可以检查所需的权限,之后生成的令牌将显示在文本框中,复制并使用它。



这个问题是,访问令牌是一个短命的(几个小时),如果你需要更长的时间,我建议您将创建一个仅应用服务器端身份验证的画布应用程序结束一个长命令(60天),那么你可以坚持它,并在那段时间使用它。


I'm using the unofficial python sdk for Facebook. This works fine for alot of graph api calls, but my recent project requires using more FQL. Certain tables (notifications for example) require a user access token rather than an app access token.

When using

graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_A‌​PP_SECRET&grant_type=client_credentials 

it seems I only get an App token back. So when calling the notification table I'll receive a "102 : A user access token is required to request this resource"

This project does not use a browser at all, and is mainly created to validate notifications, and postings generated from another app.

Whats the best way to receive a user token in this case?

Thanks N

You can not get a user access token without a direct interaction of a logged in user with your application using the facebook authentication flows. The only token that can be obtained without a user is the application token and you've got that covered already.

You can create a user token manually, the easiest way, I think, would be to use the Graph Explorer tool. Just select your application on the top right, then click the "Get Access Token" button, you can then check the needed permissions and after that a generated token will appear in the text box, copy that and use it.

The problem with that is that the access token is a short lived one (a few hours), if you need a longer one I suggest that you create a canvas application that will only implement the Server-Side authentication which will end with a long-lived token (60 days), then you can persist it and use it for that time.