如何使用服务帐户凭据将文件上传到Google Drive [英] How to upload file to google drive with service account credential

查看:301
本文介绍了如何使用服务帐户凭据将文件上传到Google Drive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google服务帐户凭据将文件上传到我的Google云端硬盘.

I wanted to upload the files to my Google Drive using a Google service account credentials.

我从Google Developer Console中以JSON文件的形式下载了凭据,并从中获取了凭据.

I downloaded credential as a JSON file from Google Developer Console, and got credential from it.

这是我的代码段.

google_drive_service = discovery.build('drive', 'v3',
          credentials=ServiceAccountCredentials.from_json_keyfile_name
                           os.path.join(settings.CLIENT_PATH, settings.CLIENT_SECRET_FILE),
                           scopes=settings.SCOPES))


media = MediaFileUpload(tmp_file_path, mimetype=tmp_file.content_type, resumable=True)
google_drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()

代码运行并且没有错误,但是我找不到上载到我的Google云端硬盘帐户的文件.我不确定为什么不上传文件.您想帮我解决这个问题吗?

The code runs and there is no error, however I can't find out the files uploaded to my Google Drive account. I am not sure why files are not uploaded. Would you like to help me to fix this problem?

推荐答案

您遇到的问题是服务帐户不是您.您已将文件上传到服务帐户Google云端硬盘帐户,而不是您的个人云端硬盘帐户.尝试做一个文件列表,您应该会看到该文件.

The issue you are having is that a service account is not you. you have uploaded a file to the service accounts Google drive account not your personal drive account. Try doing a file list you should see the file.

建议.取得服务帐户的电子邮件地址,并在您的个人Google云端硬盘帐户上共享目录,就像与其他用户共享目录一样.然后,服务帐户将能够上载到该目录.只需在上传文件后确保对其设置权限,即可授予您的个人驱动器帐户对该文件的访问权限.上传文件后,该文件将归服务帐户所有.

Suggestion. Take the service account email address and share a directory on your personal Google drive account with it like you would share with any other user. The Service account will then be able to upload to this directory. Just make sure to set the permissions on the file after you upload it granting your personal drive account access to the file. When the file is uploaded it will be owned by the service account.

这篇关于如何使用服务帐户凭据将文件上传到Google Drive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆