Search code examples
python-3.xgoogle-oauthgspread

HttpAccessTokenRefreshError, Getting this error while connecting to google sheet API through python gspread


I am trying to connect to google spread sheets through python and while getting my credentials validated i get the following error :

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.

I made sure all the following things are enabled.

  • Google sheet and google Drive APIs are enabled in my console.
  • so are Google cloud storage and Google cloud storage json APIs enabled
  • The google sheet which i am trying to perform the gspread operations on is shared with the client_email_id(mentioned in json file,downloaded after i created my credentials from google console)
  • I am behind a proxy and i am using http_proxy and https_proxy env. variables

My Code:

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())

I want to be able to connect to the google sheet through python so that i would be able to perform the read,write and update operations on the sheet. I am new to python. Sorry in advance if i made a mistake while posting the question


Solution

  • The answer is in the question

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

    It should be https://www.googleapis.com/auth/drive