在没有浏览器的情况下授权多个Google API

时间:2020-02-26 16:44:14

标签: python google-api google-bigquery google-drive-api google-sheets-api

我有一个脚本,该脚本必须从Google云端硬盘BigQuery中提取数据,然后将其通过管道传输到Google表格中。

我已经在没有浏览器的情况下设法在Bigquery和Google表格中进行了身份验证,但是我找不到关于如何在Google云端硬盘中进行身份验证的任何明确信息,更重要的是,如何以一种简洁的方式进行身份验证,因此存储服务帐户可同时使用这三个帐户。

这里是Bigquery的功能

import pandas_gbq
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_info(
    {
        -InfoService account
  },
)
credentials = credentials.with_scopes(
    [
     'https://www.googleapis.com/auth/cloud-platform',
     'https://www.googleapis.com/auth/bigquery',
     'https://www.googleapis.com/auth/drive',
     ],
     )

sql = """sqlquery"""
df = pandas_gbq.read_gbq(sql, project_id=project_id, credentials=credentials)

对于Google表格

auth_key = {
  "client_id": "id",
  "client_secret": "secret",
  "refresh_token": "token"
}

credentials = client.OAuth2Credentials(
    access_token=None,
    client_id=auth_key['client_id'],
    client_secret=auth_key['client_secret'],
    refresh_token=auth_key['refresh_token'],
    token_expiry=None,
    token_uri=GOOGLE_TOKEN_URI,
    user_agent=None,
    revoke_uri=GOOGLE_REVOKE_URI)

credentials.refresh(httplib2.Http())
credentials.authorize(httplib2.Http())
cred = json.loads(credentials.to_json())
cred['type'] = 'authorized_user'

with open('adc.json', 'w') as outfile:
  json.dump(cred, outfile)

gauth = GoogleAuth()
gauth.credentials = credentials
client = gspread.authorize(gauth.credentials)

0 个答案:

没有答案
相关问题