Can someone please help me troubleshoot setting up the Google API?

I’m real new to all of this, and I’m trying to run a Python script that’ll modify a Google spreadsheet. Based on the error messages I’ve been getting, I’m under the impression that I’ve failed in some way to configure the Google Cloud console correctly, like I haven’t given the proper credentials or something. But I have no idea what else I need to do. (In fact, I barely know what to do in the first place; this is my first time working with an API at all.)

Any tips would be greatly appreciated, thanks!

What’s the error message?

Did you try checking here:

or,

I used a different Google for Developers page-- just tried starting from scratch with a new project and following their instructions to the letter, in case I unwittingly messed something up the first time.

The error message (with some redactions for privacy) is currently:

Traceback (most recent call last):
File “C:\PATH\FILENAME”, line 17, in
spreadsheet = client.open(spreadsheet_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\PATH\client.py”, line 170, in open
self.list_spreadsheet_files(title, folder_id),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\PATH\client.py”, line 145, in list_spreadsheet_files
res = self.request(“get”, url, params=params).json()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\PATH\client.py”, line 92, in request
raise APIError(response)
gspread.exceptions.APIError: {‘code’: 403, ‘message’: ‘Request had insufficient authentication scopes.’, ‘errors’: [{‘message’: ‘Insufficient Permission’, ‘domain’: ‘global’, ‘reason’: ‘insufficientPermissions’}], ‘status’: ‘PERMISSION_DENIED’, ‘details’: [{‘@type’: ‘type.googleapis.com/google.rpc.ErrorInfo’, ‘reason’: ‘ACCESS_TOKEN_SCOPE_INSUFFICIENT’, ‘domain’: ‘googleapis.com’, ‘metadata’: {‘method’: ‘google.apps.drive.v3.DriveFiles.List’, ‘service’: ‘drive.googleapis.com’}}]}

And you enabled the Workspace API for your project?

Do you have one of these scopes in your code?

See also:

I enabled the Google Sheets API. Do I need to enable a different one as well? (I don’t see Google Workspace as an option.)

I’m not sure I understand your second question, so maybe that’s the issue. How do I find out whether or not I have those scopes in my code?

I’m not sure as you’ve not posted your code (minus the redactions for privacy). Double check your python code.
There are examples of code (in different languages, including Python) in that documentation link (spreadsheet values update). and there’s also a part in the sidebar where you can try the methods out on live code.

The whole idea of scopes is one I wasn’t familiar with 24 hours ago-- trying to give myself a crash course here on stuff I don’t entirely understand yet. Here’s the portion of the Python script that seems to me like it might be relevant:

import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials

spreadsheet_name = “NAME REDACTED”

credentials_file = “credentials.json”

scope = [‘https://www.googleapis.com/auth/spreadsheets’]
credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scope)
client = gspread.authorize(credentials)

spreadsheet = client.open(spreadsheet_name)

worksheet = spreadsheet.sheet1

df = pd.DataFrame(worksheet.get_all_records())

It can be overwhelming for sure. If it becomes too frustrating, taking a break might help too. :slight_smile:

Did you do this step first?

2. Install the Python client library for Google APIs by running
   `pip install --upgrade google-api-python-client`

and add:

from pprint import pprint

from googleapiclient import discovery

and add a HTTP request too.
ex:

[type or paste code here](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update?hl=en#http-request)

I had done that first step, and I added those next two lines, but I think the HTTP request might’ve been the issue. And that’s not something I’m confident I know how to do correctly. Here’s an updated version of the script, minus the bits that I’m confident aren’t relevant:

import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
import requests

spreadsheet_name = “NAME REDACTED”

credentials_file = “credentials.json”

scope = [‘https://www.googleapis.com/auth/spreadsheets’]
credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scope)
client = gspread.authorize(credentials)

spreadsheet = client.open(spreadsheet_name)

worksheet = spreadsheet.sheet1

df = pd.DataFrame(worksheet.get_all_records())

SKIPPED OVER A BUNCH OF CODE RELATED TO UPDATING THE SPREADSHEET

worksheet_values = worksheet.get_all_values()
values_range = f’{worksheet.title}!{worksheet_values[0][10]}:{worksheet_values[-1][10]}’

url = f"https://sheets.googleapis.com/v4/spreadsheets/637219848/values/{values_range}?valueInputOption=USER_ENTERED"

access_token = credentials.get_access_token().access_token

payload = {
“values”: worksheet_values[1:],
}

headers = {
“Authorization”: f"Bearer {access_token}",
“Content-Type”: “application/json”
}

response = requests.put(url, headers=headers, json=payload)

print(“Response Status Code:”, response.status_code)
print(“Response Content:”, response.json())

print(df)

print(“Messages have been inserted into the ‘Email text’ column of the Google Sheets spreadsheet.”)

My strong suspicion is that the issue is not with the code, but with my Google Cloud settings; I had no experience using it prior to yesterday, and it seems from the error messages like I’ve in some way failed to give myself the proper permissions, authorizations, credentials, etc.

I think that you might be right about the authorization(s) and credentials.

Right-- I think I need help troubleshooting setting up the Google API. I hope someone can help me with that!

Hi! I’m currently facing the same issue, did you ever manage to resolve this? I have been crawling the internet for a solution. Thanks