我正在尝试创建一个简单的Web应用程序,该应用程序会自动将我的数据库和媒体备份上传到指定的Google驱动器。我遵循了官方文档并创建了服务帐户凭据,为它提供了所有者角色,并从Google云平台提取了密钥(json文件)。我在我的帐户上启用了Google Drive API并编写了此代码,但是凭据。有效返回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'))
答案 0 :(得分:2)
该修改如何?
service
中的service = build('drive', 'v3', credentials=credentials)
可用于上传文件。
my file would not upload to my drive.
开始,我认为您可能对服务帐户有误解。使用服务帐户上传的文件已创建到服务帐户的云端硬盘。此云端硬盘不同于您帐户的Google云端硬盘。我认为这可能是my file would not upload to my 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云端硬盘的特定文件夹,请使用以下脚本。在这种情况下,在运行脚本之前,请与服务帐户的电子邮件共享文件夹。请注意这一点。
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.)
的错误。所以我提出了上述修改过的脚本。