1

I prepared code that should fetch data from google webmaster console new Web API (v3).

import os
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from apiclient.discovery import build
import googleapiclient
import json


client_email = '<ACCOUNT_IDENTIFIER>@<PROJECT_IDENTIFIER>.iam.gserviceaccount.com'
scopes = ['https://www.googleapis.com/auth/webmasters.readonly',
      'https://www.googleapis.com/auth/webmasters']

private_key_path = os.getcwd() + os.path.normpath('/key.p12')
http = httplib2.Http()
credentials = ServiceAccountCredentials.from_p12_keyfile(client_email,
                                                         private_key_path,
                                                         private_key_password="notasecret",
                                                         scopes=scopes
                                                        )
http_auth = credentials.authorize(http)
webmasters_service = build('webmasters', 'v3', credentials=credentials, http=http_auth)
query_params = {"startDate": "2016-03-01", "endDate": "2016-03-02"}
try:
    quered_results = webmasters_service.searchanalytics().query(
        key="<KEY>",
        siteUrl="http://<SITE_DOMAIN>/",
        body=json.dumps(query_params),
        fields="rows",
        alt="json"
    ).execute()
    print(quered_results)
except googleapiclient.errors.HttpError as e:
    print(e)

Execution results with error:

<HttpError 500 when requesting https://www.googleapis.com/webmasters/v3/sites/http%3A%2F%2F<SITE_DOMAIN>%2F/searchAnalytics/query?key=<KEY>&alt=json&fields=rows returned "Backend Error"

The code from above is for authorization with ssh key with p12 format. Key file is correct. Using client_secrets.json end up with the same error, code. The json for error is:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "Backend Error",
   }
  ],
  "code": 500,
  "message": "Backend Error"
 }
}
  • I did connect email to webmaster tools console.
  • Authorization seems to work since there is no errors for used key/account

Any ideas?

I've noticed that the same error occures when I fetch on https://developers.google.com/apis-explorer with "Request body" improperly set, but I do not see error in JSON I send. BTW It would be nice to have some validation message about that...

smentek
  • 2,820
  • 1
  • 28
  • 32

1 Answers1

0

Found it! body parameter should actually by python object, not JSON formatted string!

body=json.dumps(query_params),

Should be

body=query_params,
smentek
  • 2,820
  • 1
  • 28
  • 32