Search code examples
pythongoogle-sheetsgoogle-cloud-platformgspreadoauth2client

RefreshError while trying to access spreadsheet from python


I was trying to access my spreadsheet GetHookedTest from python following this tutorial. I've added service account successfully and here is my credentials. Shared by sheet with [email protected]

{
  "type": "service_account",
  "project_id": "gethooked",
  "private_key_id": "aaaaaabbbbbaaaabbbbaaaaa",
  "private_key": "Private Key here",
  "client_email": "[email protected]",
  "client_id": "454454554",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/orderplacer%test.iam.gserviceaccount.com"
}

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint

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

creds = ServiceAccountCredentials.from_json_keyfile_name("client_secret.json", scope)
client = gspread.authorize(creds)
sheet = client.open("GetHookedTest").sheet1

But I'm getting the RefreshError

six.raise_from(new_exc, caught_exc)
  File "<string>", line 3, in raise_from
google.auth.exceptions.RefreshError: ('No access token in response.', {'id_token': 'long string'})


Solution

  • As one method, please try the following modification.

    1. Remove the scopes of https://spreadsheets.google.com/feeds and https://www.googleapis.com/auth/drive.file.

    2. Use the Spreadsheet ID instead of Spreadsheet name.

      • From

          client.open("GetHookedTest").sheet1
        
      • To

          client.open_by_key("SpreadsheetId").sheet1
        

    Note:

    • From your reply comment, https://www.googleapis.com/auth/spreasheets is https://www.googleapis.com/auth/spreadsheets.