Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Google Sheets Api with Python

I'm currently trying to make updates to google sheets via python and I'm running into some issues with permissions.

I've been following the instructions from this Twilio guide:https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

I'm doing all of this in Jupyter, and I did save the JSON file to the proper code directory. I had no problems defining scope, creds and client.

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_id.json', scope)
client = gspread.authorize(creds)

sheet = client.open("MixIQ Tracker").sheet1

I've followed all of the steps to link the two, but I am getting this API Error with the last line.

APIError: {
"error": {
  "errors": [{
      "domain": "global",
      "reason": "insufficientPermissions",
      "message": "Insufficient Permission: Request had insufficient authentication scopes."
    }],
  "code": 403,
  "message": "Insufficient Permission: Request had insufficient authentication scopes."
 }
}

I'm not exactly sure how to resolve this. Any direction would be greatly appreciated!

like image 431
KirklandShawty Avatar asked May 10 '19 20:05

KirklandShawty


People also ask

How do I use Google Sheets API with Python?

Search for 'Google Drive API', enable it. Select Compute Engine service default, JSON, hit create. Open up the JSON file, share your spreadsheet with the "[email protected]" email listed. Save the JSON file wherever you're hosting your project, you'll need to load it in through Python later.

Can Python interface with Google Sheets?

Its Data Analysis libraries, such as Pandas and NumPy, are mostly used to manipulate and analyze data effectively. And hence, a Python to Google Sheets connect leverages NumPy or Pandas to integrate the data.


2 Answers

I believe google drive API endpoint needs to be included in your scope. I was writing data from Mailchimp API to Google Sheet.

Check this out: https://www.youtube.com/watch?v=7I2s81TsCnc> It was helpful for me.

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
like image 200
Michael deVry Avatar answered Sep 21 '22 12:09

Michael deVry


If you look at the google API scopes documentation, the scope url you are using is not referenced anywhere. This may be the problem. Try changing the scope url to https://www.googleapis.com/auth/spreadsheets.

Also, make sure the spreadsheet API is correctly enabled in your project in the google developer console.

Alternatively, you could try the Sheetfu library (I'm the author), that handles the scopes for you.

like image 30
Philippe Oger Avatar answered Sep 20 '22 12:09

Philippe Oger