Google Search Console API调用的后端错误(HttpError 500)(网站管理员/ v3)

时间:2016-03-23 14:10:29

标签: google-webmaster-tools google-api-python-client google-api-webmasters

我准备了应该从谷歌网站管理员控制台新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)

执行结果有错误:

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

上面的代码用于使用p12格式的ssh密钥进行授权。密钥文件是正确的。使用client_secrets.json最终得到相同的错误,代码。错误的json是:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "Backend Error",
   }
  ],
  "code": 500,
  "message": "Backend Error"
 }
}
  • 我确实将电子邮件连接到了网站管理员工具控制台。
  • 授权似乎有效,因为使用的密钥/帐户
  • 没有错误

有什么想法吗?

我注意到,当我使用&#34;请求正文&#34;在https://developers.google.com/apis-explorer上抓取时,会发生同样的错误。设置不当,但我没有看到我发送的JSON错误。顺便说一句,有一些关于那个......的验证信息会很好。

1 个答案:

答案 0 :(得分:0)

发现它! body参数实际上应该是python对象,而不是JSON格式的字符串!

body=json.dumps(query_params),

应该是

body=query_params,
相关问题