如何正确连接到Google Spreadsheets API?

时间:2019-02-27 00:08:44

标签: python google-sheets-api google-authentication gspread

我想使用Python连接到Google Spreadsheets API。

我这样做:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope=['https://www.googleapis.com/auth/spreadsheets', 
'https://wwww.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('cred.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open('datatest').Sheet1

print(wks.get_all_records)

但是我得到这个错误

HttpAccessTokenRefreshError: invalid_scope: https://www.googleapis.com/auth/spreadsheets is not a valid audience string.

如何使用Google-auth而不是gspread处理此数据?

1 个答案:

答案 0 :(得分:3)

您输入了4w !!!!

X'https://wwww.googleapis.com/auth/drive'

O'https://www.googleapis.com/auth/drive'

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('CredentialsFile.json', scope)
ss_app = gspread.authorize(credentials=credentials)  # make sure your Google Sheets API is enabled
sht = ss_app.open('sheet_name_xxxx')  # In your spreadsheet, click the Share button and paste the client email as same as CredentialsFile.json.client_email, and make sure your Google Drive API is enabled
wks = sht.worksheet('worksheet_name_xxxx')
print(wks.acell('A1').value)

希望有帮助。