为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁华开满天机 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 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信