Python Forum

Full Version: Google Sheets API Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I am high school senior and I want to do a small program for a school project.

I need to get information from a google sheet into my code. I followed this guide to try to do this. I followed every step and put in all my info properly, but I keep getting this error.

Error:
Traceback (most recent call last): File "C:/Users/Conner Boggan/Desktop/Senior Project/Python Code/Google Sheets/Sheets.py", line 11, in <module> sheet = client.open("tpol2").sheet1 # Open the spreadhseet File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\gspread\client.py", line 119, in open self.list_spreadsheet_files(title), File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\gspread\client.py", line 95, in list_spreadsheet_files res = self.request('get', url, params=params).json() File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\gspread\client.py", line 67, in request headers=headers, File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\requests\sessions.py", line 543, in get return self.request('GET', url, **kwargs) File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\auth\transport\requests.py", line 440, in request self.credentials.before_request(auth_request, method, url, request_headers) File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\auth\credentials.py", line 124, in before_request self.refresh(request) File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2\service_account.py", line 334, in refresh access_token, expiry, _ = _client.jwt_grant(request, self._token_uri, assertion) File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2\_client.py", line 153, in jwt_grant response_data = _token_endpoint_request(request, token_uri, body) File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2\_client.py", line 124, in _token_endpoint_request _handle_error_response(response_body) File "C:\ProgramData\Anaconda3\envs\Android app\lib\site-packages\google\oauth2\_client.py", line 60, in _handle_error_response raise exceptions.RefreshError(error_details, response_body) google.auth.exceptions.RefreshError: ('invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim.', '{"error":"invalid_grant","error_description":"Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim."}')
Here is my code for refrence:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("TPOL2.json", scope)

client = gspread.authorize(creds)

sheet = client.open("tpol2").sheet1  # Open the spreadhseet

data = sheet.get_all_records()  # Get a list of all records

print(data)
I can run each line except the last 3 which leads me to believe the code is able to get online properly, but there is something in the way between the code and the google sheet.


Any help would very much appreciated.

Thank you!
From the navigation menu, hover over APIs and Services, select Library. Search for and click on the Google Sheets API tile, Click on Enable. If your getting problem of error with APIs so you should start try to fix the error as possible,and post only the relevant sections of code.