google 服务帐户凭据无效
google service account credentials not working
我正在尝试创建一个简单的 Web 应用程序,自动将我的数据库和媒体备份上传到指定的 google 驱动器。我已经按照官方文档创建了一个服务帐户凭证,赋予它所有者角色,并从Google云平台中提取了一个密钥(json文件)。我在我的帐户上启用了 Google Drive API 并编写了这段代码,但是 credentials.valid returns 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 = build('drive', 'v3', credentials=credentials)
的 service
可用于上传文件。
- 在我的环境中,我可以确认可以使用您的脚本上传文件。
- 来自
my file would not upload to my drive.
,我认为您可能对服务帐户有误解。使用服务帐户上传的文件将创建到服务帐户的驱动器中。此驱动器与您帐户的 Google 驱动器不同。我认为这可能是 my file would not upload to my drive.
. 的原因
- 如果您想在您的 Google 驱动器中查看使用服务帐户上传的文件,需要与您的 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驱动器的“与我共享”中看到。
如果你想把你的 Google 驱动器的特定文件夹,请使用以下脚本。在这种情况下,在您 运行 脚本之前,请将文件夹与服务帐户的电子邮件共享。请注意这一点。
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.)
的错误。所以我提出了上面的修改脚本。
参考文献:
我正在尝试创建一个简单的 Web 应用程序,自动将我的数据库和媒体备份上传到指定的 google 驱动器。我已经按照官方文档创建了一个服务帐户凭证,赋予它所有者角色,并从Google云平台中提取了一个密钥(json文件)。我在我的帐户上启用了 Google Drive API 并编写了这段代码,但是 credentials.valid returns 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 = build('drive', 'v3', credentials=credentials)
的service
可用于上传文件。- 在我的环境中,我可以确认可以使用您的脚本上传文件。
- 来自
my file would not upload to my drive.
,我认为您可能对服务帐户有误解。使用服务帐户上传的文件将创建到服务帐户的驱动器中。此驱动器与您帐户的 Google 驱动器不同。我认为这可能是my file would not upload to my drive.
. 的原因
- 如果您想在您的 Google 驱动器中查看使用服务帐户上传的文件,需要与您的 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驱动器的“与我共享”中看到。
如果你想把你的 Google 驱动器的特定文件夹,请使用以下脚本。在这种情况下,在您 运行 脚本之前,请将文件夹与服务帐户的电子邮件共享。请注意这一点。
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.)
的错误。所以我提出了上面的修改脚本。