<p>下面是我用来让OAuth2身份验证与Picasa一起工作的代码。首先,您需要通过googledeveloper控制台在<a href="https://console.developers.google.com/" rel="noreferrer">https://console.developers.google.com/</a>创建一个客户机ID,然后必须将客户机机密作为JSON下载并将文件名传递给OAuth2Login。在</p>
<p>第一次运行此代码时,必须通过web浏览器对客户端进行授权,并将获得的代码粘贴到应用程序中。然后,凭证存储在凭证存储区指定的文件中。在</p>
<pre><code>def OAuth2Login(client_secrets, credential_store, email):
scope='https://picasaweb.google.com/data/'
user_agent='myapp'
storage = Storage(credential_store)
credentials = storage.get()
if credentials is None or credentials.invalid:
flow = flow_from_clientsecrets(client_secrets, scope=scope, redirect_uri='urn:ietf:wg:oauth:2.0:oob')
uri = flow.step1_get_authorize_url()
webbrowser.open(uri)
code = raw_input('Enter the authentication code: ').strip()
credentials = flow.step2_exchange(code)
storage.put(credentials)
if (credentials.token_expiry - datetime.utcnow()) < timedelta(minutes=5):
http = httplib2.Http()
http = credentials.authorize(http)
credentials.refresh(http)
gd_client = gdata.photos.service.PhotosService(source=user_agent,
email=email,
additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token})
return gd_client
</code></pre>