Search code examples
javascriptgmail-apigoogle-api-js-client

Request had insufficient authentication scopes javascript


I already have a project to query google calendar apis. I wanted to go further by querying google mail apis. In my project I have activated mail API enter image description here I have added discoveryDocs in my javascript app like this

[
"https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest",
"https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"
]

And scopes like this

"https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events https://mail.google.com/"

Initialization of my client is done like this:

initClient(): void {
gapi.client.init({
  apiKey: 'ma_api_key',
  clientId: 'my_client_id.apps.googleusercontent.com',
  discoveryDocs: [
    "https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest",
    "https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"
  ],
  scope: 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events https://mail.google.com/',
}).then(() => {
  console.log('ok')
});

}

I still can get my calendars and events but I can not get my labels with this code:

getMails(): void {
console.log(gapi);
gapi.client.gmail.users.labels.list({
  'userId': 'me'
}).then(function(response) {
  var labels = response.result.labels;

  console.log(labels);
});

}

What am I missing please ?

Thanks


Solution

  • Request had insufficient authentication scopes javascript

    Means that the user who you have authentication with has authorized your application to use some scopes but they are not the scopes, but you are trying to use a method which requires addental scopes then the user has authorized your application for.

    This error normally occurs when you authorize your application once then change the scopes and run it again, if your application still has a session var or cookies from the previous authorization request then your application will run without requesting access of the user and adding the additional scopes.

    You need to revoke the access token or force the application show the consent screen again