首页 > 解决方案 > 在没有浏览器活动的情况下使用 Python API 在 Google 驱动器上上传文件

问题描述

我正在使用 Google Drive API 上传视频,然后获取该视频的 URL。

这是我尝试上传文件的代码:

SCOPES = ['https://www.googleapis.com/auth/drive.file']
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Now build our api object, thing
drive_api = build('drive', 'v3', credentials=creds)
file_metadata = {
    'name': 'Test file',
    # 'mimeType': 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload('/path/to/file',
                        mimetype='text/text',
                        resumable=True)
file = drive_api.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print('File ID: %s' % file.get('id'))

我的最终目标是通过服务器上的一些调度程序在 Google Drive 上上传文件。

我还尝试了上传文件的教程。

但是上面的代码和教程总是打开浏览器并获得权限。我想在没有浏览器活动的情况下上传文件。

如何在没有浏览器活动的情况下上传文件,例如仅使用api_keyclient_id等?

标签: pythondjangogoogle-drive-api

解决方案


推荐阅读