facebook.GraphAPIError:必须使用有效的访问令牌查询当前用户的信息
我一直在尝试打印我的好友列表,但总是遇到这个错误。我很确定我已经提供了一个令牌。有没有什么建议?
import facebook
import urllib
import urllib2
import json
import urlparse
import subprocess
import warnings
import urllib2
import json
#https://developers.facebook.com/tools/access_token/
# Parameters of your app and the id of the profile you want to mess with.
FACEBOOK_APP_ID = '{app_id}'
FACEBOOK_APP_SECRET = '{app_secret}'
FACEBOOK_PROFILE_ID = 'my-id-number-here'
# Trying to get an access token. Very awkward.
oauth_args = dict(client_id = FACEBOOK_APP_ID,
client_secret = FACEBOOK_APP_SECRET,
grant_type = 'client_credentials')
oauth_curl_cmd = ['curl',
'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)]
oauth_response = subprocess.Popen(oauth_curl_cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE).communicate()[0]
try:
oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0]
except KeyError:
print('Unable to grab an access token!')
exit()
facebook_graph = facebook.GraphAPI(oauth_access_token)
# Print friends list.
profile = facebook_graph.get_object("me")
friends = facebook_graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
2 个回答
2
可能是你的短期访问令牌已经过期了。你可以通过以下链接来检查这个问题:
https://developers.facebook.com/tools/debug/accesstoken/
在这个网站上,你可以查看访问令牌的有效性。
5
你遇到这个错误是因为你使用的是访问令牌,而不是用户令牌。
用户令牌可以通过Facebook开发者工具生成。
https://developers.facebook.com/tools/explorer/145634995501895/
只需点击“获取令牌” > “获取用户访问令牌”,这样就能解决问题了。