Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webmasters API User does not have sufficient permission for site

I use google-api-php-client library to access webmaster tools data. When I wanted to list sitemaps, it appeared Fatal error: Uncaught exception 'Google_Service_Exception'(403) User does not have sufficient permission for site. See also: https://support.google.com/webmasters/answer/2451999.' I add the service account email address as a restrict user for my site, but error still exists.

Finally I find the answer: A service account is not like a regular Google account. You cannot use it to access specific resources even if you give that specific address "access" to it. See here for the different ways you can authorize your requests to the Webmaster API.

like image 971
Janmay Avatar asked Jan 22 '15 02:01

Janmay


2 Answers

For me the problem was not the actual permission, but the way the domain name is passed. Here is my Node.js example, but the same goes for PHP:

import {JWT} from 'google-auth-library';

const client = new JWT(
    null,
    'service-account.json',
    null,
    ['https://www.googleapis.com/auth/webmasters.readonly'],
);
const res = await client.request({
    url: 'https://www.googleapis.com/webmasters/v3/sites/sc-domain:yourdomain.com/searchAnalytics/query',
    method: 'POST',
    data: {
        "startDate": "2020-04-01",
        "endDate": "2020-05-01",
        "dimensions": ["country", "device"]
    }
});

console.log(res.data);

Notice the syntax sc-domain:yourdomain.com. Passing 'yourdomain.com' will result in the error User does not have sufficient permission for site yourdomain.com.

  • Make sure you added the service-account email as user in search console: https://search.google.com/search-console/users
like image 113
MartijnvdB Avatar answered Oct 17 '22 02:10

MartijnvdB


A service account is not like a regular Google account. You cannot use it to access specific resources even if you give that specific address "access" to it.

You need to manage the service permissions via Webmaster Admin. Add your service account

[email protected]

there.

like image 42
Decebal Avatar answered Oct 17 '22 03:10

Decebal