facebook.GraphAPIError:必须使用活动访问令牌来查询有关当前用户的信息

时间:2014-07-27 20:37:17

标签: python facebook

我一直在尝试打印朋友列表一段时间后出现此错误。我相当肯定我提供了一个令牌。有什么想法吗?

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 个答案:

答案 0 :(得分:1)

您收到此错误的原因是您使用的是访问令牌而非用户令牌。

可以使用Facebook开发人员生成用户令牌

https://developers.facebook.com/tools/explorer/145634995501895/

只需点击获取令牌>获取用户访问令牌 这对我来说很有把握

答案 1 :(得分:0)

可能您的短期访问令牌已过期。您可以通过

查看

https://developers.facebook.com/tools/debug/accesstoken/

并查看访问令牌的有效性。