簡體   English   中英

使用 pydrive 從 OAUTH 游樂場谷歌實現訪問令牌

[英]Implement access token from OAUTH playground google with pydrive

我試圖在不使用 pydrive 請求谷歌驅動器權限的情況下進行身份驗證,他們建議我在堆棧上遵循這個答案答案但是當我得到訪問令牌時,我真的不知道在哪里放置它,我嘗試了 client_secret.json 但是沒用。

您關注的問題來自2013 年,在任何舊的問題始終是廢話之后。

首先,使用Oauth2 Playground僅用於測試和開發。 如果您不使用自己的客戶端 ID 和客戶端密碼,在 Oauth 操場上創建的令牌將很快過期。 如果您使用自己的客戶端 ID 和客戶端密碼,那么您的刷新令牌將在 7 天內過期。 由於您不擁有該域,因此無法使用 Oauth 操場的重定向 uri 驗證應用程序。 所有這些安全保護措施都是在 2013 年之前實施的。

假設您有一個單用戶應用程序並且您只會訪問自己的驅動器帳戶,那么您應該使用服務帳戶

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

scope = ["https://www.googleapis.com/auth/drive"]
gauth = GoogleAuth()
gauth.auth_method = 'service'
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope)
drive = GoogleDrive(gauth)

about = drive.GetAbout()

print('Current user name:{}'.format(about['name']))
print('Root folder ID:{}'.format(about['rootFolderId']))
print('Total quota (bytes):{}'.format(about['quotaBytesTotal']))
print('Used quota (bytes):{}'.format(about['quotaBytesUsed']))


file_list = drive.ListFile().GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

您應該咨詢 pydrive github 頁面服務帳戶或我們的官方google api python 示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM