我能夠使用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. 生成令牌以訪問Google帳戶
- 3. GAE - 存儲多個OAuth訪問令牌
- 4. 使用OAuth 2.0訪問Gmail Imap訪問令牌
- 5. OAuth:存儲訪問令牌和祕密
- 6. 取得OAuth 2.0訪問令牌的AdSense
- 7. 存儲訪問令牌
- 8. oauth訪問令牌以獲得新的訪問令牌
- 9. 存儲用戶的Facebook訪問令牌
- 10. 在Android應用程序中存儲Facebook訪問令牌
- 11. Google OAuth - 如何重用訪問令牌
- 12. 從oauth的twitter應用程序獲取訪問令牌
- 13. 應用程序訪問令牌和用戶訪問令牌之間的區別
- 14. 如何在Unity Android應用程序中使用OAuth 2.0獲取訪問令牌?
- 15. 將OAuth訪問令牌保存在用戶Cookie中可以嗎?
- 16. 在Javascript客戶端(例如Angular)中存儲OAuth訪問令牌
- 17. Abraham's Twitter oAuth PHP庫 - 在WordPress數據庫中存儲用戶訪問令牌?
- 18. Google Contacts API - 獲取訪問令牌(oauth)
- 19. OAuth v2(Google API)失效訪問令牌
- 20. 訪問令牌和刷新令牌在Google服務帳戶中的驅動器api中爲空
- 21. 在Windows應用程序中的Dwolla oauth訪問令牌
- 22. 的OAuth 2訪問令牌
- 23. WP7 C#檢索Google OAuth 2.0請求的訪問令牌
- 24. 在Ajax應用程序中獲取Twitter oauth訪問令牌
- 25. facebook用戶訪問令牌與應用訪問令牌
- 26. Twitter訪問令牌存儲
- 27. 在oauth上緩存訪問令牌?
- 28. 無法從OAuth 2.0獲取訪問令牌授權碼流程
- 29. 以角度存儲OAuth 2.0的令牌
- 30. OAuth 2.0 - 何時應該使用刷新令牌更新訪問令牌?
我不在Google App Engine上工作腳本應該在服務器上運行,讀取一些文件並根據這些文件中的數據填充數據庫。我只是想配置它第一次即粘貼鏈接它的瀏覽器,然後獲得授權代碼,粘貼回到終端。數據將始終從相同的用戶id讀取。dat後,即使腳本停止,當我重新啓動它應該從storage.How做我做的。 – Navi 2013-03-20 11:06:12