简体   繁体   English

python:GraphAPIError:无效的OAuth访问令牌

[英]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.

相关问题 Python中Marketing API上的“无效的OAuth访问令牌” - “Invalid OAuth access token” on Marketing API in Python Evernote - OAuth 访问令牌 - Python - Evernote - OAuth access token - Python 访问令牌过期时Gmail API错误(Python,Django):HttpAccessTokenRefreshError invalid_client:未找到OAuth客户端 - Gmail API error when access token expires (Python, Django): HttpAccessTokenRefreshError invalid_client: The OAuth client was not found Python:身份验证凭据异常:“请求具有无效的身份验证凭据。预期的 OAuth 2 访问令牌” - Python: authentication credentials Exception: "Request had invalid authentication credentials. Expected OAuth 2 access token" facebook.GraphAPIError:必须使用活动访问令牌来查询有关当前用户的信息 - facebook.GraphAPIError: An active access token must be used to query information about the current user Python 使用访问令牌请求 OAuth2 header - Python Requests OAuth2 header with access token GraphAPIError:必须使用活动访问令牌来查询有关当前用户的信息 - GraphAPIError: An active access token must be used to query information about the current user 在尝试撤销 OAuth2 访问令牌(Google 登录)时获取 invalid_token 和令牌过期或撤销 - Getting invalid_token & Token expired or revoked while trying to revoke OAuth2 access token (Google Sign in) 访问令牌和刷新令牌,在Python中使用Google Plus提供无效授权? - Access Token and Refresh token giving invalid grant in Google Plus in Python? 使用OAuth 2.0和Python请求检索Yahoo API的访问令牌 - Retrieve access token for Yahoo API using OAuth 2.0 and Python requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM