我在google中的oauth2-Authorization存在问题,尤其是第2步:将代码交换为访问令牌。这是我的代码:
import httplib2
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from apiclient.discovery import build
import simplejson as json
import cgi
...
#initiating the flow
flow = OAuth2WebServerFlow(client_id='<client_id>',
client_secret='<client_secret>',
scope='https://www.googleapis.com/auth/drive',
redirect_uri='<url_to_this_script')
#initiating storing
storage = Storage('credentials_store.dat')
credentials = storage.get()
#checks, if there is a code already transmitted
if form.has_key("code"):
code = form["code"].value
#step 2: if so, then try to get credentials with the code
#HERE IS THE PROBLEM
credentials = flow.step2_exchange(code)
storage.put(credentials)
if credentials is None or credentials.invalid:
#step 1: get the code
auth_uri = flow.step1_get_authorize_url()
print "Location:%s" % auth_uri
print # to end the CGI response headers.
所以我试着解释一下:
如果没有&#34;代码&#34;在参数中,当调用脚本时,将传递第一个if-then-else-check并且程序从步骤1开始。这部分没问题。用户将被重定向到他可以授权脚本的页面。
授权后将再次调用该脚本,但这次使用&#34;代码&#34;在参数中。如谷歌开发人员参考中所述,我尝试使用此命令获取凭据
凭据= flow.step2_exchange(代码)
在此步骤之前它工作正常但随后它会停止并显示错误消息:
oauth2client.client.FlowExchangeError: invalid_request
我做错了什么?谢谢你的帮助!
P.S。:如果您对此感兴趣,该应用程序只需访问我自己在google驱动器中的文档。
编辑: 以下是完整的错误消息:
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] No handlers could be found for logger "oauth2client.client"
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] Traceback (most recent call last):
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] File "/is/htdocs/wp1108372_VJATWIO8DR/www/cgi-bin/ref_ScopeGetter2.py", line 84, in [module]
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] http = auth_http("Axel","Drive")
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] File "/is/htdocs/wp1108372_VJATWIO8DR/www/cgi-bin/ref_ScopeGetter2.py", line 76, in __init__
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] self.auth_http = self.get_auth_http(user,scope)
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] File "/is/htdocs/wp1108372_VJATWIO8DR/www/cgi-bin/ref_ScopeGetter2.py", line 51, in get_auth_http
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] credentials = flow.step2_exchange(code)
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] File "/is/htdocs/wp1108372_VJATWIO8DR/www/cgi-bin/oauth2client/client.py", line 910, in step2_exchange
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] raise FlowExchangeError(error_msg)
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] oauth2client.client.FlowExchangeError: invalid_request
[Thu Jul 03 07:14:07 2014] [error] [client 31.19.230.167] Premature end of script headers: ref_ScopeGetter2.py