Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google api (sheets) Request had insufficient authentication scopes

i want to read an write data from a sheet, reading works fine but writing doesn't. i use all the scopes mentioned in the documentation: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

data_writer(1,1,1)

code:

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'+"https://www.googleapis.com/auth/drive.file"+"https://www.googleapis.com/auth/drive"
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = apiclient.discovery.build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
SPREADSHEET_ID = '1JwVOqtUCWBMm_O6esIb-9J4TgqAmMIdYm9sf5y-A7EM'
RANGE_NAME = 'Jokes!A:C'
# How the input data should be interpreted.
value_input_option = 'USER_ENTERED'  
# How the input data should be inserted.
insert_data_option = 'INSERT_ROWS' 

def data_reader():
    #reading data
    read = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,range=RANGE_NAME).execute()
    #reading values
    values = read.get('values', [])
    if not values:
        print('No data found.')
    else:

        for row in values:
            print(row[2])
            continue
def data_writer(score,num_comments,mystring):
    value_range_body = {
        "score":score,
        "num_comments":num_comments,
        "joke":mystring
    }
    request = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
    response = request.execute()
like image 572
extreme4all Avatar asked Jul 15 '18 15:07

extreme4all


People also ask

How do I fix request had insufficient authentication scopes?

Knowledge Base "Request had insufficient authentication scopes." This error will occur if you miss an important step during the login process. To fix, login to YouTube again via vMix. But when you reach the below step make sure you tick the last checkbox for "Manage your YouTube Account".

Had insufficient authentication scopes?

This error occurs when you do not select a required authentication item when adding your account for Analytics Edge use. When the login authentication screen opens in your browser, make sure to select (check) all of the boxes offered in the authorization request.


2 Answers

SCOPES must be of type list

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

Side note: you have

.../auth/drive

And

.../auth/drive.file

/drive.file limits the API to work with only drive.file API but /drive opens up to all of the drive API. So you should pick one that fits your needs.

Side note 2: Based on the link you’ve provided, it mentions you need at least one of the API’s to work with spreadsheets, so you may not need all of them either.

like image 114
skarchmit Avatar answered Sep 28 '22 05:09

skarchmit


First off you should only need https://www.googleapis.com/auth/drive as it gives full access to a users drive account including reading and writing sheets.

  • list of Sheet scopes
  • list of drive scopes

If you have already run your code once and authenticated your user then changed the scopes in your code. Remember that you will need to run your code again and re-authenticate the user to gain the access granted by the new scopes.

Side note:

SCOPES = 'https://www.googleapis.com/auth/spreadsheets'+"https://www.googleapis.com/auth/drive.file"+"https://www.googleapis.com/auth/drive"

Is just going to be one long string you need to separate them with a space or as the other answer states use an array.

SCOPES = 'https://www.googleapis.com/auth/spreadsheets ' + "https://www.googleapis.com/auth/drive.file " + "https://www.googleapis.com/auth/drive"
like image 34
DaImTo Avatar answered Sep 28 '22 04:09

DaImTo