我能够使用python为Google Drive执行quickstart.py。但是,我们如何存储令牌并再次使用它 - 在没有提示用户的情况下再次为同一个用户使用。在Google驱动器上发送文件请求时,我们可以通过某种方式将用户与访问令牌进行映射。从Google OAuth 2.0存储访问令牌以访问应用程序帐户中的驱动器数据
5
A
回答
8
google-api-python-client
,some其中welldocumented有很多不同的Storage
类型。
一些例子:
oauth2client.file.Storage:
from oauth2client.file import Storage
...
storage = Storage('a_credentials_file')
storage.put(credentials)
...
credentials = storage.get()
oauth2client.keyring_storage.Storage:
from oauth2client.keyring_storage import Storage
...
storage = Storage('application name', 'user name')
storage.put(credentials)
...
credentials = storage.get()
oauth2client.appengine.StorageByKeyName:
from oauth2client.keyring_storage import StorageByKeyName
from oauth2client.keyring_storage import CredentialsNDBModel
...
storage = StorageByKeyName(CredentialsNDBModel, some_user_id, 'credentials')
storage.put(credentials)
...
credentials = storage.get()
oauth2client.django_orm.Storage:
from django.contrib.auth.models import User
from oauth2client.django_orm import Storage
from your_project.your_app.models import CredentialsModel
...
user = # A User object usually obtained from request.
storage = Storage(CredentialsModel, 'id', user, 'credential')
credential = storage.get()
...
storage.put(credential)
3
我觉得你应该给信贷bossylobster一个更完整的答案,但基于您的评论,而这恰恰是我的设置,我已经扩充使用的quickstart.py存储类别:
#!/usr/bin/python
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
# Copy your credentials from the console
CLIENT_ID = 'PASTE_YOUR_ID'
CLIENT_SECRET = 'PASTE_YOUR_SECRET'
# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Create a credential storage object. You pick the filename.
storage = Storage('a_credentials_file')
# Attempt to load existing credentials. Null is returned if it fails.
credentials = storage.get()
# Only attempt to get new credentials if the load failed.
if not credentials:
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
storage.put(credentials)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
# Use 'drive_service' for all of the API calls
相关问题
- 1. 生成令牌以访问Google帐户
- 2. GAE - 存储多个OAuth访问令牌
- 3. 使用OAuth 2.0访问Gmail Imap访问令牌
- 4. OAuth:存储访问令牌和秘密
- 5. 存储访问令牌
- 6. 存储用户的Facebook访问令牌
- 7. Google OAuth - 如何重用访问令牌
- 8. 从oauth的twitter应用程序获取访问令牌
- 9. 将OAuth访问令牌保存在用户Cookie中可以吗?
- 10. Abraham's Twitter oAuth PHP库 - 在WordPress数据库中存储用户访问令牌?
- 11. 访问令牌和刷新令牌在Google服务帐户中的驱动器api中为空
- 12. Google Contacts API - 获取访问令牌(oauth)
- 13. OAuth v2(Google API)失效访问令牌
- 14. 在Windows应用程序中的Dwolla oauth访问令牌
- 15. 的OAuth 2访问令牌
- 16. 在Ajax应用程序中获取Twitter oauth访问令牌
- 17. Twitter访问令牌存储
- 18. 无法从OAuth 2.0获取访问令牌授权码流程
- 19. 以角度存储OAuth 2.0的令牌
- 20. OAuth 2.0 - 何时应该使用刷新令牌更新访问令牌?
- 21. 无法获取Google OAuth 2.0访问令牌
- 22. Instagram OAuth访问令牌到期的应用程序
- 23. 访问令牌的Google+
- 24. 获取用户访问令牌facebook访问令牌节点
- 25. 如何自动登录到谷歌API来获取的OAuth 2.0令牌来访问已知的用户帐户
- 26. 在Windows 8应用程序中从Goodreads获取访问令牌
- 27. 无法从Google Container VM映像访问服务帐户的元数据令牌
- 28. instagram api应用程序访问令牌
- 29. 访问令牌的Exchange Oauth请求令牌失败Google API
- 30. G套件帐户,访问用户的驱动器
我不在Google App Engine上工作脚本应该在服务器上运行,读取一些文件并根据这些文件中的数据填充数据库。我只是想配置它第一次即粘贴链接它的浏览器,然后获得授权代码,粘贴回到终端。数据将始终从相同的用户id读取。dat后,即使脚本停止,当我重新启动它应该从storage.How做我做的。 – Navi 2013-03-20 11:06:12