[英]python: GraphAPIError: Invalid OAuth access token
I'm making a web app in python using the Flask framework to request the access token from Facebook using the SDK supplied in their site . 我正在使用Flask框架在python中创建一个Web应用程序,以使用其站点中提供的SDK从Facebook请求访问令牌。
The access token is returned and it is correctly set in the GraphAPI object. 返回访问令牌,并在GraphAPI对象中正确设置。 However, it is returning the following error: 但是,它返回以下错误:
GraphAPIError: Invalid OAuth access token. GraphAPIError:无效的OAuth访问令牌。
If I query the graph API from my local python environment using the same access token, it works just fine. 如果我使用相同的访问令牌从我的本地python环境查询图形API,它就可以正常工作。 The problem seems to be when executing in the webserver. 问题似乎是在网络服务器中执行时。
See code snippet below: 请参阅下面的代码段:
@app.route('/facebook')
def fb():
if 'token' in session:
graph = facebook.GraphAPI(session['token'])
return graph.get_object("me")
@app.route('/facebook/login')
def fblogin():
code = request.args.get('code','')
if(code == ""):
args = dict(client_id=app_id, redirect_uri=request.base_url)
#args = dict(client_id=app_id, redirect_uri=request.url_root + 'facebook')
return redirect(
"https://graph.facebook.com/oauth/authorize?" +
urllib.urlencode(args))
else:
token = facebook.get_access_token_from_code(code, request.base_url, app_id, app_secret)
session['token'] = [token.itervalues().next()]
return redirect (request.url_root + 'facebook')
Has anyone faced this before and/or can provide some insights? 有没有人在此之前和/或可以提供一些见解?
Ok, 2 issues that I have managed to correct in this code and get it working: 好的,我已经设法在此代码中纠正了2个问题并使其正常工作:
1) The following line of code makes a list, that why the GraphAPI object is not able to identify a valid access token: 1)以下代码行列出了一个列表,说明为什么GraphAPI对象无法识别有效的访问令牌:
session['token'] = [token.itervalues().next()]
2) The following line of code gives an error stating that 'dict' is not callable. 2)以下代码行给出了一个错误,指出'dict'不可调用。 This is because the returned variable is a dictionary and, in order to be returned as a view, one must first transform it into a string: 这是因为返回的变量是一个字典,为了作为视图返回,必须先将其转换为字符串:
return graph.get_object("me")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.