[英]Google drive api with python
我正在嘗試制作一個應用程序,它可以在我的 google drive 文件夾中顯示文件和文件夾,所以我去了google apis並使用了我在下面的頁面上找到的代碼
因為我沒想到它只是讓我登錄到我的驅動器然后它返回了一個錯誤我正在為它提供一張照片這是返回的錯誤它說
Message='client_secret' Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: 文件“D:\Study\Computer Science\G10\Paython 2nd semester\ python consol app\python consol app\python_consol_app.py",第 48 行,在 main() 中
我設法跳過這個錯誤並重命名 json 文件,我解決了這個錯誤只是為了找到另一個錯誤說
Message=授權用戶信息不是預期格式,缺少字段 client_secret、client_id、refresh_token。 源=D:\Study\Computer Science\G10\Paython 第二學期\python consol app\python consol app\python_consol_app.py StackTrace:文件“D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py",第 48 行,在 main() 中
所以我想知道如果我的應用程序可以運行到每個文件夾和子文件夾,我應該如何解決這個問題
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('drive', 'v3', credentials=creds)
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
if __name__ == '__main__':
main()
第一:啟用 API 在使用谷歌APIs之前,你需要在谷歌雲項目中開啟它們。 您可以在單個 Google Cloud 項目中啟用一個或多個 API。 在谷歌雲控制台中,啟用 Gmail API。 從這里
第二:為桌面應用程序授權憑據要以最終用戶身份進行身份驗證並訪問應用程序中的用戶數據,您需要創建一個或多個 OAuth 2.0 客戶端 ID。 客戶端 ID 用於識別 Google 的 OAuth 服務器的單個應用程序。 如果您的應用程序在多個平台上運行,您必須為每個平台創建一個單獨的客戶端 ID。 在 Google Cloud 控制台中,go 轉到菜單菜單 > API 和服務 > 憑據。 Go 到憑據
單擊創建憑據 > OAuth 客戶端 ID。 單擊應用程序類型 > 桌面應用程序。 在名稱字段中,輸入憑證的名稱。 此名稱僅顯示在 Google Cloud 控制台中。 單擊創建。 出現 OAuth 客戶端創建屏幕,顯示您的新客戶端 ID 和客戶端密碼。 單擊確定。 新創建的憑據顯示在 OAuth 2.0 客戶端 ID 下。
最后下載 JSON 文件並將文件重命名為 credentials.json,並將文件移動到您的工作目錄。
我的問題解決了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.