Python:显示无效令牌的Facebook API [英] Python: Facebook API showing invalid token
问题描述
我正在使用 facepy facebook graph API 访问我的邮箱/邮件,我遵循以下两种方法:
I am using facepy facebook graph API to access my mailbox/messages and I followed the following two approaches:
第一种方法
我使用访问令牌
我从 Graph Explorer
facebook页面获得,并使用以下代码:
I used the access token
I got from Graph Explorer
facebook page and use the below code:
from facepy import GraphAPI
graph = GraphAPI(token)
print graph.get('/me')
#Rest of the code
上述代码工作正常,我可以使用 FQL查询
。当我的auth_token 过了
之后,问题出现了。
The above code worked fine and I was able to retrieve all my messages using an FQL Query
. The problem arised when my auth_token expired
after sometime.
所以,经过一些搜索之后,我转向了两个
So, after some googling I shifted to approach two:
现在,我所做的是创建一个Facebook应用程序给它 read_mailbox
权限,并得到它是 id和key
,然后使用 get_application_access_token 获取令牌的方法。
Now, what I did was created a facebook app gave it read_mailbox
permission and got it's id and key
and then used get_application_access_token method of facepy to get the token.
检索到我使用的令牌后:
After retrieving the token I used:
token = facepy.utils.get_application_access_token(app_id, key)
graph.get('/me')
## OUT: OAuthError: [2500] An active access token must be used to query information about the current user.
facepy.utils.get_extended_access_token(token, app_id, key)
# OUT: OAuthError: [1] No user access token specified
现在,您可以看到在使用应用程序令牌时生成的错误( commented#
)。
Now, you can see the error(commented #
) generated on using the application token.
我相信我得到的错误是因为Facebook需要 user_token
,而且我提供了 app_token
。
I believe the error I am getting is because facebook needs the user_token
and I am supplying it with app_token
.
所以,是否可以使用app_token访问用户数据,如果不能发送一个扩展令牌
可以访问用户数据。
So, is it possible to access user data using the app_token and if not how can one issue a extended token
which can access user data.
更新:
所以,我遵循@Johannes的建议,并尝试过这个错误:
So, I followed @Johannes suggestion and tried this but ran into error:
from facepy.utils import get_extended_access_token
from facepy import GraphAPI
token = "My user access token got from https://developers.facebook.com/tools/explorer"
long_lived_access_token = get_extended_access_token(token)
graph = GraphAPI(long_lived_access_token)
graph.get('/me')
现在,当我运行上面的代码我得到
Now, when I ran the above code I got
TypeError: get_extended_access_token() takes exactly 3 arguments (1 given)
所以,我将它改为 long_lived_access_token = get_extended_access_token(令牌,无,无)
和得到
So, I changed it to long_lived_access_token = get_extended_access_token(token, None, None)
and got
facepy.exceptions.OAuthError
所以,我再次将它改为 long_lived_access_token = get_extended_access_token(token,app_id,key)
,我得到与上述相同的异常/错误
So, I again I changed it to long_lived_access_token = get_extended_access_token(token, app_id, key)
and I got the same exception/error as above.
所以,这是一个错误还是我做错了。
So, is this a bug or am I doing something wrong.
PS:我安装了最新的
PS: I installed the latest version from git.
推荐答案
您假设您无法使用应用程序访问令牌来读取用户的邮箱,但是您所获得的错误源于您尚未初始化图表
使用访问令牌。
You're right in your assumption that you cannot use application access tokens to read a user's mailbox, but the error you're getting stems from the fact that you haven't initialized graph
with an access token at all.
可以这样说,您正在寻求如何扩展用户访问令牌的正确轨道。正如你已经发现的那样,Facepy HEAD(很快成为0.9版)具有函数 get_extended_access_token
,它接受现有的短命令用户访问令牌并将其扩展。扩展的用户访问令牌持续2个月,您可以在Facebook的文档中阅读更多关于他们的信息,例如删除offline_access权限。
Be that as it may, you're on the right track in asking for how you can extend the user's access token. As you have already discovered, Facepy HEAD (soon to be version 0.9) has a function get_extended_access_token
which accepts an existing short-lived user access token and extends it. Extended user access tokens last for 2 months, and you can read more about them in Facebook's documentation on the removal of offline_access permission.
如果要立即使用 get_extended_access_token
,您将有从git安装facepy:
If you want to use get_extended_access_token
right now, you will have to install facepy from git:
$ pip install git+git://github.com/jgorset/facepy.git@b5153f460f2f52cef9a5e49a3b48b3fb8742356c
安装正确版本的Facepy后,您可以扩展现有的短期用户访问权限令牌并初始化 GraphAPI
的新实例:
Once you've installed the right version of Facepy, you can extend an existing short-lived user access token and initialize a new instance of GraphAPI
with it:
from facepy.utils import get_extended_access_token
from facepy import GraphAPI
long_lived_access_token, expires_at = get_extended_access_token(short_lived_access_token, application_id, application_secret_key)
graph = GraphAPI(long_lived_access_token)
graph.get('/me')
这篇关于Python:显示无效令牌的Facebook API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!