OAuth 2.0 序列类似于以下内容(给定为您注册的应用程序适当定义的应用程序常量)。
生成请求令牌。
token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=" ".join(SCOPES),
user_agent=USER_AGENT)
授权请求令牌。对于一个简单的命令行应用程序,您可以执行以下操作:
print 'Visit the following URL in your browser to authorise this app:'
print str(token.generate_authorize_url(redirect_url=REDIRECT_URI))
print 'After agreeing to authorise the app, copy the verification code from the browser.'
access_code = raw_input('Please enter the verification code: ')
获取访问令牌。
token.get_access_token(access_code)
创建一个 gdata 客户端。
client = gdata.docs.client.DocsClient(source=APP_NAME)
授权客户。
client = token.authorize(client)
您可以通过执行以下操作保存访问令牌以供以后使用(因此避免在令牌再次过期之前执行手动身份验证步骤):
f = open(tokenfile, 'w')
blob = gdata.gauth.token_to_blob(token)
f.write(blob)
f.close()
下次开始时,您可以通过执行以下操作重用保存的令牌:
f = open(tokenfile, 'r')
blob = f.read()
f.close()
if blob:
token = gdata.gauth.token_from_blob(blob)
然后,对身份验证序列的唯一更改是您通过指定 refresh_token 参数将此令牌传递给 OAuth2Token:
token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=" ".join(SCOPES),
user_agent=USER_AGENT,
refresh_token=token.refresh_token)
希望这可以帮助。花了一段时间来解决它:-)。