亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

谷歌服務帳戶憑據不起作用

谷歌服務帳戶憑據不起作用

繁華開滿天機 2023-06-13 15:17:59
我正在嘗試創建一個簡單的網絡應用程序,自動將我的數據庫和媒體備份上傳到指定的谷歌驅動器。我按照官方文檔創建了一個服務帳戶憑證,賦予它所有者角色,并從谷歌云平臺提取了一個密鑰(json 文件)。我在我的帳戶上啟用了 Google Drive API 并編寫了這段代碼,但 credentials.valid 返回 False 并且我的文件不會上傳到我的驅動器。from google.oauth2 import service_accountimport googleapiclient as googlefrom googleapiclient.http import MediaFileUpload, HttpRequestfrom googleapiclient.discovery import buildSCOPES = ['https://www.googleapis.com/auth/drive']credentials = service_account.Credentials.from_service_account_file('./service-credentials.json', scopes=SCOPES)print(credentials.valid)service = build('drive', 'v3', credentials=credentials)file_metadata = {'name' : 'python.png'}media = MediaFileUpload('./python.png', mimetype='image/png')file_up = service.files().create(body=file_metadata, media_body=media, fields='id').execute()file_back = service.files().get(fileId=file_up['id']).execute()print(file_back.get('WebContentLink'))
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

這個改裝怎么樣?

修改點:

  • 我認為在您的腳本中,serviceofservice = build('drive', 'v3', credentials=credentials)可用于上傳文件。

    • 在我的環境中,我可以確認可以使用您的腳本上傳文件。

    • my file would not upload to my drive.,我認為您可能對服務帳戶有誤解。使用服務帳戶上傳的文件將創建到服務帳戶的驅動器中。此云端硬盤與您帳戶的 Google 云端硬盤不同。我認為這可能是 的原因my file would not upload to my drive.。

    • 如果您想在您的 Google Drive 中查看使用服務帳戶上傳的文件,則需要將上傳的文件與您的 Google 帳戶共享?;蛘撸枰獙⑽募蟼鞯侥?Google Drive 中與服務帳戶共享的文件夾中。

  • 而且,在您的腳本中,file_back.get('WebContentLink')使用了。在這種情況下,None總是返回,因為WebContentLink需要是WebContentLink。而且,在 Drive API v3 中,默認返回值不包括webContentLink. 所以需要設置fields。

當以上幾點反映到您的腳本中時,您的腳本將如下所示。

修改腳本:

from google.oauth2 import service_account

import googleapiclient as google

from googleapiclient.http import MediaFileUpload, HttpRequest

from googleapiclient.discovery import build


SCOPES = ['https://www.googleapis.com/auth/drive']

credentials = service_account.Credentials.from_service_account_file('./service-credentials.json', scopes=SCOPES)


service = build('drive', 'v3', credentials=credentials)

file_metadata = {'name': 'python.png'}

media = MediaFileUpload('./python.png', mimetype='image/png')

file_up = service.files().create(body=file_metadata, media_body=media, fields='id').execute()


# Create a permission. Here, your Google account is shared with the uploaded file.

yourEmailOfGoogleAccount = '###'  # <--- Please set your Email address of Google account.

permission = {

    'type': 'user',

    'role': 'writer',

    'emailAddress': yourEmailOfGoogleAccount,

}

service.permissions().create(fileId=file_up['id'], body=permission).execute()


file_back = service.files().get(fileId=file_up['id'], fields='webContentLink').execute()  # or fields='*'

print(file_back.get('webContentLink'))

當您運行上述腳本時,上傳的文件可以在您的 Google 云端硬盤中的“與我共享”中看到。


如果您想放置 Google Drive 的特定文件夾,請使用以下腳本。在這種情況下,在運行腳本之前,請將文件夾與服務帳戶的電子郵件共享。請注意這一點。


  from google.oauth2 import service_account

  import googleapiclient as google

  from googleapiclient.http import MediaFileUpload, HttpRequest

  from googleapiclient.discovery import build


  SCOPES = ['https://www.googleapis.com/auth/drive']

  credentials = service_account.Credentials.from_service_account_file('./service-credentials.json', scopes=SCOPES)


  service = build('drive', 'v3', credentials=credentials)

  file_metadata = {'name': 'python.png', 'parents': ['###']}  # <--- Please set the folder ID shared with the service account.

  media = MediaFileUpload('./python.png', mimetype='image/png')

  file_up = service.files().create(body=file_metadata, media_body=media, fields='id').execute()


  file_back = service.files().get(fileId=file_up['id'], fields='webContentLink').execute()  # or fields='*'

  print(file_back.get('webContentLink'))

筆記:

現階段服務賬號上傳文件的屬主發生變更時,會出現類似You can't yet change the owner of this item. (We're working on it.). 所以我提出了上面的修改腳本。


查看完整回答
反對 回復 2023-06-13
  • 1 回答
  • 0 關注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號