2

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

Leustad
  • 375
  • 1
  • 5
  • 20

1 Answers1

1

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...

Leustad
  • 375
  • 1
  • 5
  • 20
  • which version of `oauth2client` are you using? I tried to `from oauth2client.service_account import ServiceAccountCredentials` but it doesn't look like `ServiceAccountCredentials` is included in my version of `oauth2client.service_account`. I see a `ServiceAccountCredentials` class... – Abundnce10 May 02 '16 at 23:10
  • Im using `google-api-python-client (1.5.0)`. That's version came with `pip install --upgrade google-api-python-client`. Hope it helps. [Doc](https://developers.google.com/webmaster-tools/v3/quickstart/quickstart-python#step_1_enable_the_search_console_api) – Leustad May 03 '16 at 13:26
  • I originally ran `pip install --upgrade oauth2client` but since I had an old version of `pip` it wouldn't upgrade properly. I'm up-to-date with `pip` and now `oauth2client` has the `ServiceAccountCredentials` class. Thanks! – Abundnce10 May 03 '16 at 17:47
  • 1
    My email has multiple SearchConsole sites associated with it. Instead of placing this new email (in the `keyfile.json`) I delegated my email credentials using `delegated_credentials = credentials.create_delegated('user@example.org')` and then `http_auth = delegated_credentials.authorize(Http())`, which gave me access to all of the sites my email address has access to. – Abundnce10 May 03 '16 at 18:15
  • ohh I was looking for a way to do that. Thank you for the tip – Leustad May 04 '16 at 01:08