Janmay
Janmay

Reputation: 121

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.

Upvotes: 12

Views: 9667

Answers (4)

mr_faulty
mr_faulty

Reputation: 166

If anyone is reading this in 2023: @MartijnvdB's solution is still valid, use sc-domain:domain.com for domain properties, or https://www.myurl.com for non-domain properties. Documentation: https://developers.google.com/webmaster-tools/v1/searchanalytics/query#parameters (see siteUrl).

Upvotes: 3

MartijnvdB
MartijnvdB

Reputation: 1002

For me the problem was not the actual permission, but the way the domain name is passed.

You should prefix your domain with sc-domain:: sc-domain:yourdomain.com. Passing just 'yourdomain.com' without the sc-domain: prefix will result in the error User does not have sufficient permission for site yourdomain.com.

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);

Upvotes: 23

SirJ
SirJ

Reputation: 183

The solution suggested by @MartijnvdB won't work. But finally, I got one that works for me:

URL should be composed as follows: sc-domain:domain.com

Upvotes: 2

Decebal
Decebal

Reputation: 1419

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.

Upvotes: 3

Related Questions