HttpAccessTokenRefreshError,通过python gspread连接到Google Sheet API时出现此错误

时间:2019-01-03 05:17:10

标签: python-3.x google-oauth gspread

我正在尝试通过python连接到Google电子表格,并且在验证我的凭据时出现以下错误:

Error:
   Traceback (most recent call last):
   File 
   "C:\Users\skansal\Desktop\Agile_Tool\Agile_Google_Sheets\Google_Api.py", 
   line 10, in 
   gs = gspread.authorize(credentials)
   File "C:\Users\skansal\AppData\Local\Programs\Python\Python36\lib\site- 
   packages\gspread_init_.py", line 38, in authorize
   client.login()
   File "C:\Users\skansal\AppData\Local\Programs\Python\Python36\lib\site- 
   packages\gspread\client.py", line 51, in login
   self.auth.refresh(http)
   File "C:\Users\skansal\AppData\Local\Programs\Python\Python36\lib\site- 
   packages\oauth2client\client.py", line 545, in refresh
   self._refresh(http)
   File "C:\Users\skansal\AppData\Local\Programs\Python\Python36\lib\site- 
   packages\oauth2client\client.py", line 749, in _refresh
   self._do_refresh_request(http)
   File "C:\Users\skansal\AppData\Local\Programs\Python\Python36\lib\site- 
   packages\oauth2client\client.py", line 819, in _do_refresh_request
   raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
   oauth2client.client.HttpAccessTokenRefreshError: invalid_scope: 
   http://www.googleapis.com/auth/drive is not a valid audience string.

我确保启用了以下所有功能。

  • 我的控制台中启用了Google表格和Google Drive API。
  • 启用了Google云存储和Google云存储json API
  • 我要对其执行gspread操作的Google工作表是 与client_email_id共享(在json文件中提及,我下载后 从Google控制台创建了我的凭据)
  • 我在使用代理,并且正在使用 http_proxy https_proxy env。变量

我的代码:

from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import gspread
scope = ['http://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credential_token.json',scope)

gs = gspread.authorize(credentials)
worksheet = gc.open('Project_Sheet').sheet1
print(wks.get_all_records())

我希望能够通过python连接到Google工作表,这样我就可以在工作表上执行读取,写入和更新操作。我是python的新手。抱歉,如果我在发布问题时出错了

1 个答案:

答案 0 :(得分:2)

答案在问题中

invalid_scope: http://www.googleapis.com/auth/drive is not a valid audience string.

应为https://www.googleapis.com/auth/drive