谷歌服务帐户凭据不起作用

我正在尝试创建一个简单的网络应用程序,自动将我的数据库和媒体备份上传到指定的谷歌驱动器。我按照官方文档创建了一个服务帐户凭证,赋予它所有者角色,并从谷歌云平台提取了一个密钥(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'))


繁华开满天机
浏览 151回答 1
1回答

长风秋雁

这个改装怎么样?修改点:我认为在您的脚本中,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.&nbsp;所以需要设置fields。当以上几点反映到您的脚本中时,您的脚本将如下所示。修改脚本: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)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 = '###'&nbsp; # <--- Please set your Email address of Google account.permission = {&nbsp; &nbsp; 'type': 'user',&nbsp; &nbsp; 'role': 'writer',&nbsp; &nbsp; 'emailAddress': yourEmailOfGoogleAccount,}service.permissions().create(fileId=file_up['id'], body=permission).execute()file_back = service.files().get(fileId=file_up['id'], fields='webContentLink').execute()&nbsp; # or fields='*'print(file_back.get('webContentLink'))当您运行上述脚本时,上传的文件可以在您的 Google 云端硬盘中的“与我共享”中看到。如果您想放置 Google Drive 的特定文件夹,请使用以下脚本。在这种情况下,在运行脚本之前,请将文件夹与服务帐户的电子邮件共享。请注意这一点。&nbsp; from google.oauth2 import service_account&nbsp; import googleapiclient as google&nbsp; from googleapiclient.http import MediaFileUpload, HttpRequest&nbsp; from googleapiclient.discovery import build&nbsp; SCOPES = ['https://www.googleapis.com/auth/drive']&nbsp; credentials = service_account.Credentials.from_service_account_file('./service-credentials.json', scopes=SCOPES)&nbsp; service = build('drive', 'v3', credentials=credentials)&nbsp; file_metadata = {'name': 'python.png', 'parents': ['###']}&nbsp; # <--- Please set the folder ID shared with the service account.&nbsp; media = MediaFileUpload('./python.png', mimetype='image/png')&nbsp; file_up = service.files().create(body=file_metadata, media_body=media, fields='id').execute()&nbsp; file_back = service.files().get(fileId=file_up['id'], fields='webContentLink').execute()&nbsp; # or fields='*'&nbsp; print(file_back.get('webContentLink'))笔记:现阶段服务账号上传文件的属主发生变更时,会出现类似You can't yet change the owner of this item. (We're working on it.). 所以我提出了上面的修改脚本。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python