我正在寻找一种构建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()
答案 0 :(得分:0)
将我的代码更改为此:
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
store = file.Storage('credentials.json')
flow = client.OAuth2WebServerFlow(client_id='619103408544-qbpfk38g9jk4tkc5gshvds9hs8g5ur9o.apps.googleusercontent.com',
client_secret='faJQr2Wd3x25_yKYIWslxR4s',
scope=SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
使用了一个不同的函数来创建流程,事实证明,您实际上不需要使用商店来制作凭据,所以我只删除了store.get()行。