question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using application-default credentials for Google Search Console API does not provide sufficient permissions.

See original GitHub issue

As the title states, using the application default credentials to access Google Search Console data does not seem to work.

Note: My Google Account has delegated ownership of a site.

    /** 
     * Acquires application default settings from Google's Auth library.
     *
     * @returns {Object} authClient
     */
    function _authenticateWithGoogle(){
      console.log("Authenticating...");
      return new Promise(function(resolve, reject){
        google.auth.getApplicationDefault(function(err, authClient) {
          if (err) {
            console.error('Authentication failed because of ', err);
            reject(err);
          }
          if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            const scopes = [
              // Needed to read data from google search console.
              'https://www.googleapis.com/auth/webmasters.readonly'
            ];
            authClient = authClient.createScoped(scopes);
          }
          // For debugging purposes.
          console.log(JSON.stringify(authClient));
          // Resolve authClient obj.
          resolve(authClient);
        });
      });
    };

Using the above code to generate an auth client to then run the following:

webmaster.sites.list({auth: authClient}, function(err, resp) {
  if (err){
    console.error(err);
      reject(err);
  } else {
    console.log(resp);
    resolve();
  }
});

The above code errors out and returns a not very helpful message:

  code: 403,
  errors:
   [ { domain: 'global',
       reason: 'insufficientPermissions',
       message: 'Insufficient Permission' } ] }

I spent hours scouring the web trying to find out what might be going wrong, but unfortunately found nothing related to application-default credentials and search console. It seems that the Search Console API just does not work with default-application credentials.

I have since created a new service-account and used JWT based authentication and was able to query the Search Console API successfully.

Considering the webmasters API is part of googleapis, I would have expected that the getApplicationDefault functionality would have been sufficient to create the authClient necessary to access data, but it seems not to be the case.

Anyone attempting to use application-default to access the Search Console API – stick with JWT, save yourself the trouble until they get a fix in.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JustinBeckwithcommented, Apr 4, 2018

I’m seeing the same exact result FWIW. Digging deeper.

1reaction
bllevycommented, Apr 4, 2018

My process was:

  • Create a service account in GCP
  • Acquire private key + email of service acocunt
  • Use the google.auth.JWT library to authenticate based on that service account’s email, private key, and scope
    • scope used: https://www.googleapis.com/auth/webmasters.readonly

Use the require('googleapis').webmasters('v3') client object to query the search console API via webmaster.searchanalytics.query().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Search Console API: How do I Solve "User does not ...
Can anyone help me understand why I'm getting the following error when I run my code? { message: "User does not have sufficient...
Read more >
Prerequisites | Search Console API - Google Developers
Your account must have the appropriate Search Console permission on a given property in order to call that method on that property. For...
Read more >
Permission issue when querying analytics using search ...
Recently, this process began getting the error 403 - "User does not have sufficient permission for site <our domain>". Accessing the "Sites: list"...
Read more >
Method: iamPolicies.lintPolicy | IAM Documentation
https://console.developers.google.com/apis/api/iam // 2. This sample uses Application Default Credentials for authentication. // If not already done, ...
Read more >
Authentication - pandas-gbq - Read the Docs
A common problem with default credentials when running on Google Compute Engine is that the VM does not have sufficient scopes to query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found