【发布时间】:2018-07-11 21:04:43
【问题描述】:
我正在寻找一种方法来构建 Google Credentials 对象并访问我的 Google 表格电子表格,而无需引用包含我的 client_secret.json 数据的另一个文件。这感觉应该很容易做到,我只想能够将 JSON 复制到我的 python 脚本中并以这种方式访问它,但我一直无法找到一种方法来做到这一点。
根据https://oauth2client.readthedocs.io/en/latest/source/oauth2client.file.html,似乎使用当前方法的唯一方法是使用文件路径,但如果文件实际上只包含 JSON,似乎应该有一种方法将 JSON 放入我的 python脚本并从那里使用它。
下面列出的是我目前如何从我的 json 文件中获取值。
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
SPREADSHEET_ID = ID
RANGE_NAME = sheetName + '!A2:D'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
【问题讨论】:
-
我有点困惑,但如果你想在脚本中保留你的信任,你可以在其中制作一个 dict。
-
如果您从deprecated
oauth2client包切换到google-auth/google-auth-oauth包,您可以使用Flow.from_client_config方法和来自client_secrets的信息文件。
标签: python google-api google-oauth google-api-python-client oauth2client