Google Drive API Python服务帐户示例 [英] Google Drive API Python Service Account Example

查看:97
本文介绍了Google Drive API Python服务帐户示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Google服务帐户而不是OAuth流针对Google Drive API进行身份验证?

Is it possible to authenticate against the Google Drive API using a Google Service Account, rather than an OAuth flow ?

Google云端硬盘的Python示例使用OAuth- Google云端硬盘Python快速入门

The Python examples for Google Drive use OAuth - Google drive Python Quick start

但是我找不到任何服务帐户示例.

However I can't find any Service Account examples.

我使用的其他大多数Google API(翻译,Cloud Vision)使用都使用服务帐户,因此,为了保持一致性,我想弃用我的Google Drive OAuth代码.

The majority of the other Google APIs I use (Translate, Cloud Vision) do use Service Account however, so I'd like to deprecate my Google Drive OAuth code for consistency.

推荐答案

我知道的最佳服务帐户python示例是

The best service account python example that i know of is the one for Google analytics

应该是这样的.

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


SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_drive():
  """Initializes an service object.

  Returns:
    An authorized service object.
  """
  creds = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  service = build('drive', 'v3', credentials=creds)

  return service

一旦有了驱动器服务,您就应该能够使用其他教程中剩下的代码.只是不同的auth方法.

Once you have the drive service you should be able to use the rest of the code you have from the other tutorial. Its just the auth method that is diffrent.

仅记得创建服务帐户凭据而不是Oauth凭据.

Just remember to create service account credentials and not Oauth credentials.

这篇关于Google Drive API Python服务帐户示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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