简体   繁体   English

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

[英]How to obtain a user access token in Python

I'm using the unofficial python sdk for Facebook. 我正在使用Facebook的非官方python sdk。 This works fine for alot of graph api calls, but my recent project requires using more FQL. 这适用于很多图形api调用,但我最近的项目需要使用更多的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. 好像我只得到一个App令牌。 So when calling the notification table I'll receive a "102 : A user access token is required to request this resource" 因此,当调用通知表时,我将收到“102:请求此资源需要用户访问令牌”

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. 如果没有使用facebook身份验证流程登录用户与您的应用程序的直接交互,您将无法获得用户访问令牌。 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 . 您可以手动创建用户令牌,我认为最简单的方法是使用Graph Explorer工具 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. 问题在于访问令牌是一个短暂的(几个小时),如果你需要更长的一个,我建议你创建一个只能实现服务器端身份验证的画布应用程序。生活令牌(60天),然后你可以坚持它并在那个时候使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM