Search code examples
pythongoogle-search-consolegoogle-api-python-clientgoogle-api-webmasters

OAuth 2.0 Server to server Credentials authorization Fail(Search Console - Webmaster tools)


I'm trying to use OAuth 2.0 for Server to Server application for google webmaster tools(Search Console) so I've followed the instructions here.

This Application is NOT on Google App Engine or Google Compute Engine

Created a service account and enabled domain-wide delegation. Downloaded the .json file and stored it to the root of the script.

Sample:

from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from httplib2 import Http    

scopes = ['https://www.googleapis.com/auth/webmasters.readonly']

credentials = ServiceAccountCredentials.from_json_keyfile_name(
        'keyfile.json', scopes=scopes)

http_auth = credentials.authorize(Http())

webmasters_service = build('webmasters', 'v3', http=http_auth)

site_list = webmasters_service.sites().list().execute()
print(site_list)

But I'm getting

{} Empty dataset. Even if I change the email address in the keyfile.json. This tells me that the file is not getting used somehow. So the attempt the get the lists of the sites in the account resulting as Empty.

If I do

site_list = webmasters_service.sitemaps().list(siteUrl="www.example.com").execute()

I get:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/webmasters/v3/sites/www.example.com/sitemaps?alt=json returned "User does not have sufficient permission for site 'http://www.example.com/'. See also: https://support.google.com/webmasters/answer/2451999.">

Which again tells me that, this account has no right to get the sitemaps of the given URL because it doesn't has the appropriate permissions.

This Account is the owner account and the service account has owner permissions.

Any Ideas?

Thank you


Solution

  • I hate to answer my own question but here is how I made it to work;

    So bottom line is, on the

    and

    ... tutorials someone forgot to mention about adding the newly generated e-mail address to the app's permission's section...