簡體   English   中英

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

[英]google service account credentials not working

我正在嘗試創建一個簡單的 Web 應用程序,它會自動將我的數據庫和媒體備份上傳到指定的谷歌驅動器。 我已經按照官方文檔創建了一個服務帳戶憑證,賦予它所有者角色,並從谷歌雲平台提取了一個密鑰(json文件)。 我在我的帳戶上啟用了 Google Drive API 並編寫了此代碼,但credentials.valid 返回 False 並且我的文件不會上傳到我的驅動器。

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)

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'))

這個改裝怎么樣?

改裝要點:

  • 我認為在您的腳本中, service service = build('drive', 'v3', credentials=credentials)可用於上傳文件。
    • 在我的環境中,我可以確認可以使用您的腳本上傳文件。
    • my file would not upload to my drive. ,我以為您可能對服務帳戶有誤解。 使用服務帳戶上傳的文件將創建到服務帳戶的 Drive。 此雲端硬盤與您帳戶的 Google 雲端硬盤不同。 我認為這可能是my file would not upload to my drive. .
    • 如果您想在您的 Google Drive 上查看使用服務帳戶上傳的文件,則需要與您的 Google 帳戶共享上傳的文件。 或者,需要將文件上傳到與服務帳戶共享的 Google 雲端硬盤中的文件夾。
  • 而且,在您的腳本中,使用了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 Drive 的“與我共享”中看到上傳的文件。

  • 如果您想放置 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.) You can't yet change the owner of this item. (We're working on it.) 所以我提出了上面的修改腳本。

參考:

暫無
暫無

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

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