python - 来自服务器(后端)的带有 Python 的 Google Drive API,无需浏览器身份验证

标签 python google-drive-api

我想将我的 Saas 应用程序的文件保存到我的 Google 云端硬盘帐户,我看到的所有示例都是使用 oauth2 身份验证并且需要最终用户身份验证打开浏览器,我需要从我的服务器上传文件无需任何用户交互,直接将文件发送到我的帐户!

我已经尝试了很多我在网上找到的教程,但都没有成功,主要是官方的

Google Drive API with Python

如何以编程方式从我的服务器进行身份验证并上传文件并使用共享文件夹等 API 功能?

我正在使用 Python,库 PyDrive使用相同的方法进行验证

最佳答案

你可以这样做,但需要使用 Service Account ,它是(或者更确切地说可以用作)应用程序的帐户,不需要浏览器打开。

文档在这里:https://developers.google.com/api-client-library/python/auth/service-accounts

还有一个示例(没有 PyDrive,它只是所有这些的包装器,但使服务帐户有点棘手):

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http

scopes = ['https://www.googleapis.com/auth/drive.readonly']

credentials = ServiceAccountCredentials.from_json_keyfile_name('YourDownloadedFile-5ahjaosi2df2d.json', scopes)

http_auth = credentials.authorize(Http())
drive = build('drive', 'v3', http=http_auth)

request = drive.files().list().execute()
files = request.get('items', [])
for f in files:
    print(f)

关于python - 来自服务器(后端)的带有 Python 的 Google Drive API,无需浏览器身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46457093/

相关文章:

python - 进度条动画在 cygwin 中不显示,在 cmd 中正常

python - Python 中的匈牙利算法

google-drive-api - 从 Perl 访问 Google Drive SDK

iphone - 列出 Google 云端硬盘中的所有文件夹内容

python - 使用 Drive python API 在 Google 电子表格中设置 "publish to web"

python - 将 Julian 日期转换为数据框中的正常日期?

python - 当 sqlite3 数据库写入中断时会发生什么?

python - 如何安排一对不同的函数以便真正并行运行?

python - 使用 pydrive 转换为 Google 驱动程序更新文件给出 : ApiRequestError: HttpError 500 Error

Google Drive API V3 如何正确转义查询参数?