jtomasrl
jtomasrl

Reputation: 1451

Google spreadsheet api Request had insufficient authentication scopes

I'm making an script to read data from google spreadsheet using the following script on nodejs:

var url = oauth2Client.generateAuthUrl({
    access_type:     'offline',
    approval_prompt: 'force',
    scope: [
      'https://www.googleapis.com/auth/analytics.readonly',
      'https://www.googleapis.com/auth/drive',
      'https://www.googleapis.com/auth/spreadsheets'
    ]
});
global.googleapis = { expiry_date: 0 };
google.options({ auth: oauth2Client });
var sheets    = google.sheets('v4');
sheets.spreadsheets.get({ spreadsheetId: 'id'}, function(err, data) {
    res.send(err, data);
});

But on every get request i'm getting this error:

Request had insufficient authentication scopes.

I checked the Google developers console to enable the Google Drive API and it's enabled, so I dont really know what could it be.

Upvotes: 62

Views: 123481

Answers (12)

Jordan Yankovich
Jordan Yankovich

Reputation: 1

Using a newer version of the google sheets npm module I had to delete the local file "token.json" to re-trigger the auth and it worked.

Also, make sure you change your "scopes" to have auth permissions instead of read-only.

Upvotes: -1

amandeep singh
amandeep singh

Reputation: 47

Select proper permission granting access to google sheets read, edit, etc, when OAuth popup open for asking google account permissions

Upvotes: -1

hhrzc
hhrzc

Reputation: 2051

In my case (Java + IntellijIdea) I deleted a file: ~/IdeaProjects/projectName/tokens/StoredCredential and restarted code.

This folder was created with GoogleAuthorizationCodeFlow.Builder, so may be different, but the file should be the same.

After restarting I have switched to the Quickstart page again for access managing - give all access for the user and follow.

After that, all work well for me

Upvotes: 0

Gunay Anach
Gunay Anach

Reputation: 1222

as @herrogokunaruto mentioned, ensure token.pickle file is not present in the workspacefolder

Upvotes: -1

hbear
hbear

Reputation: 97

Also please check the installed version of googleapis in your workspace, because the latest version (51) does not support for Node 8. So you need downgrade its version. https://github.com/googleapis/google-api-nodejs-client/releases

Upvotes: -1

Long Nguyen
Long Nguyen

Reputation: 11024

  1. Firstly delete the credentials files ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json (depending on your setting)

  2. Change the scope variable used for reading cells from Google Spreadsheets from

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];

to

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];

  1. After the execution of code, API will authenticate again and then the issue will be resolved.

Upvotes: 102

Hasan Sawan
Hasan Sawan

Reputation: 421

Delete the token from tokens folder and try again. If you have tried to run the Google Drive Quickstart before running the Spreadsheet Quickstart, the existing token might not have the spreadsheet access updated.

Upvotes: 1

herrogokunaruto
herrogokunaruto

Reputation: 21

In my case a token.pickle file was created in the same working directory as my program, I also had my credentials.json file in the same directory, all I did was deleted the token.pickle file , then changed the scope, then ran it again, it will again ask for authentication in your browser and that's it , it works.

My code snippet is like below , I was creating a pickle file so I had to delete it before changing scope

    if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

Upvotes: 2

Ajay
Ajay

Reputation: 31

I was looking for the credential location for long time. Lot of forum mentioned that it'd be under c:\users\.credentials\google-api****.json But I couldn't find them anywhere. Then I figured out that my program created a token.pickle file right in the program .py directory which is essentially the credential file.

Upvotes: 0

王雅晶
王雅晶

Reputation: 61

The same problem occurred when I updated the scope. Along with the instruction, I delete the token.json file under the same directory with the python script, then the Authentication scope issue went away.

Upvotes: 0

Ken H
Ken H

Reputation: 641

The scopes look good, maybe you should try to remove the credentials stored previously in /Users/yourUserName/.credentials/sheets.googleapis.com-projectName/*, and then execute the application again to get new credentials.

Upvotes: 25

KENdi
KENdi

Reputation: 7781

First, make sure you also enable the Sheets API in your Developers Console.

The insufficient authentication scopes is an error in the OAuth 2.0 token provided in the request specifies scopes that are insufficient for accessing the requested data.

So make sure you use the correct and all necessary scope and check this Authorizing requests with OAuth 2.0 if you properly follow the steps here.

Lastly, try to revoke the access and try to redo it.

For more information, check this related SO question:

Upvotes: 8

Related Questions