KirklandShawty
KirklandShawty

Reputation: 263

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!

Upvotes: 12

Views: 5006

Answers (4)

Jack
Jack

Reputation: 1

You only need this scope

scope = ["https://www.googleapis.com/auth/drive"]

Upvotes: 0

Michael deVry
Michael deVry

Reputation: 206

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']

Upvotes: 19

Iddos
Iddos

Reputation: 127

Use this as your scope:

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

Before calling the "ServiceAccountCredentials"

You will need to enable the Google Drive API in the "console.cloud.google" as well as the sheets API.

Upvotes: 2

Philippe Oger
Philippe Oger

Reputation: 1098

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.

Upvotes: 3

Related Questions