问题描述
我正在尝试将Google Docs API与Python + Django和OAuth 2结合使用.我已经通过google-api-python-client获得了OAuth访问令牌等,其代码实际上是从
I'm trying to use the Google Docs API with Python+Django and OAuth 2. I've got the OAuth access token, etc. via google-api-python-client, with the code essentially copied from http://code.google.com/p/google-api-python-client/source/browse/samples/django_sample/plus/views.py
现在,我假设我应该使用Google gdata API 2.0.17版.如果是这样,我将找不到确切的方法来授权使用gdata客户端进行的查询. http://packages.python上的文档. org/gdata/docs/auth.html#upgrading-to-an-access-token (无论如何似乎已经过时了),比如说要将客户端上的auth_token属性设置为gdata.oauth.OAuthToken的实例.如果是这种情况,我应该将哪些参数传递给OAuthToken?
Now, I assume I should be using the google gdata API, v 2.0.17. If so, I'm unable to find exactly how to authorize queries made using the gdata client. The docs at http://packages.python.org/gdata/docs/auth.html#upgrading-to-an-access-token (which appear outdated anyway), say to set the auth_token attribute on the client to an instance of gdata.oauth.OAuthToken. If that's the case, what parameters should I pass to OAuthToken?
简而言之,我正在寻找一个有关如何授权给定OAuth访问令牌的,如何授权使用gdata API进行查询的示例.
In short, I'm looking for a brief example on how to authorize queries made using the gdata API, given an OAuth access token.
推荐答案
OAuth 2.0序列类似于以下内容(已为您注册的应用程序适当定义了应用程序常量).
The OAuth 2.0 sequence is something like the following (given suitably defined application constants for your registered app).
-
生成请求令牌.
Generate the request token.
token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=" ".join(SCOPES),
user_agent=USER_AGENT)
授权请求令牌.对于简单的命令行应用程序,您可以执行以下操作:
Authorise the request token. For a simple command-line app, you can do something like:
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: ')
获取访问令牌.
Get the access token.
token.get_access_token(access_code)
创建一个gdata客户端.
Create a gdata client.
client = gdata.docs.client.DocsClient(source=APP_NAME)
授权客户端.
Authorize the client.
client = token.authorize(client)
您可以执行以下操作,保存访问令牌以供以后使用(因此避免执行手动身份验证步骤,直到令牌再次到期):
You can save the access token for later use (and so avoid having to do the manual auth step until the token expires again) by doing:
f = open(tokenfile, 'w')
blob = gdata.gauth.token_to_blob(token)
f.write(blob)
f.close()
下次启动时,您可以通过以下方式重复使用保存的令牌:
The next time you start, you can reuse the saved token by doing:
f = open(tokenfile, 'r')
blob = f.read()
f.close()
if blob:
token = gdata.gauth.token_from_blob(blob)
然后,对身份验证序列的唯一更改是,您通过指定refresh_token参数将此令牌传递给OAuth2Token:
Then, the only change to the authentication sequence is that you pass this token to OAuth2Token by specifying a refresh_token argument:
token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=" ".join(SCOPES),
user_agent=USER_AGENT,
refresh_token=token.refresh_token)
希望这会有所帮助.花了一段时间才解决:-).
Hope this helps. It took a while to work it out :-).
这篇关于使用Google文档列表API,Python和OAuth 2进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!