简体   繁体   English

使用相同的credentials.json 在python 中访问google drive/google sheet api

[英]use the same credentials.json to access google drive/google sheet api in python

I am write interface to access google api with google drive and google sheets.我正在编写使用谷歌驱动器和谷歌表访问谷歌api的接口。 For each api, I follow google api like google drive to create a credentials.json and using following code for no problem.对于每个 api,我按照google api像 google drive 一样创建一个credentials.json并使用以下代码没有问题。 But as interface, how can I just use one credentials.json file to access multiple api?但是作为接口,我如何只使用一个credentials.json文件来访问多个api?

from googleapiclient.discovery import build
from oauth2client import client, tools
flow = client.flow_from_clientsecrets('~/credentials.json', ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'])
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))

As long as the project on google developer console has the diffrent apis enabled you can use the same file for which ever apis you want to access your just going to have to request access for each of them.只要 google 开发者控制台上的项目启用了不同的 api,您就可以对要访问的任何 api 使用相同的文件,您只需为每个 api 请求访问权限。

from googleapiclient.discovery import build
from oauth2client import client, tools
flow = client.flow_from_clientsecrets('~/credentials.json', 
              ['https://spreadsheets.google.com/feeds',
              'https://www.googleapis.com/auth/drive'])
creds = tools.run_flow(flow, store)
serviceDrive = build('drive', 'v3', credentials=creds)
serviceSheets = build('sheets', 'v4', credentials=creds)

You then access each of your apis though their own service.然后,您可以通过他们自己的服务访问您的每个 api。 The user will be prompted to authenticate for both drive, and sheets when they login.当用户登录时,系统将提示用户对驱动器和工作表进行身份验证。

sheet = serviceSheets.spreadsheets()
results = serviceDrive.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM