对google.webmaster.api的Power Query调用,发布,请求问题

时间:2019-04-11 11:45:02

标签: powerquery google-api-webmasters

我通过Power-Query(M)调用了google.webmasters.api,并成功配置了oath2,并首次成功调用了get&list。 现在,我尝试调用/ searchAnalytics / query吗?仅适用于Post。 这总是以400错误响应。查询或网址的格式无法正常工作。

还有一些其他信息:

Power Query - Reference

Google Webmaster Api - Reference

PowerBi Community

格式日期不同:

body = "{ ""startDate"": ""2019-01-01"", ""endDate"": ""2019-02-02"" }",

body = "{ ""startDate"": ""2019/01/01"", ""endDate"": ""2019/02/02"" }",

let
    body = "{ ""startDate"": ""2019-01-01"", ""endDate"": ""2019-02-02"" }",
    AccessTokenList = List.Buffer(api_token),
    access_token = AccessTokenList{0},
    AuthKey = "Bearer " & access_token,
    url = "https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fxxxxxxxxx.xxx/searchAnalytics/query?",
    Response = Web.Contents(url, [Headers=[Authorization=AuthKey, ContentType="application/json", Accept="application/json"], Content=Text.ToBinary(body) ]),
    JsonResponse = Json.Document(Response)
in
    Response

在Gooogle-Api概述中获得400,并显示为400通话

有什么想法吗?

Thx

1 个答案:

答案 0 :(得分:0)

确保请求标头有效。服务器需要Content-Type标头,而不是ContentType

文档(https://developers.google.com/webmaster-tools/search-console-api-original/v3/searchanalytics/query#try-it)建议请求应类似于:

POST https://www.googleapis.com/webmasters/v3/sites/[SITEURL]/searchAnalytics/query HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json

{}

所以主要的收获是:

  1. 必须使用HTTP POST方法
  2. URL必须有效
    • 您尚未提供实际的URL,因此您必须自己进行验证。我将摆脱?中尾随url的影响(因为您不包括查询字符串-即使您已经包含了查询字符串,也应将它们传递到的Query字段中options记录,而不是自己构建查询字符串。
  3. 标题(AuthorizationAcceptContent-Type)应该是有效/存在的。
    • 在分隔表达式中构建标题。然后将该表达式传递到Headers记录的options字段。这使您有机会查看/检查标题(以确保它们符合预期)。
  4. 正文应包含有效的JSON,以传递给API方法。

总而言之,您的M代码可能类似于:

let
    // Some other code is needed here, in which you define the expression api_token
    AccessTokenList = List.Buffer(api_token),
    access_token = AccessTokenList{0},
    AuthKey = "Bearer " & access_token,
    requestHeaders = [Authorization = AuthKey, #"Content-Type" = "application/json", Accept = "application/json"],
    parametersToPost = [startDate = "2019-01-01", endDate = "2019-02-02"], // Can include other parameters here e.g. dimensions, as mentioned in Search Console API documentaton.
    jsonToPost = Json.FromValue(parametersToPost, TextEncoding.Utf8), // Second argument not required (as is default), but just be explicit until you've got everything working.
    url = "https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fxxxxxxxxx.xxx/searchAnalytics/query", // Uri.EscapeDataString function can be use for URL encoding
    response = Web.Contents(url, [Headers=requestHeaders, Content=jsonToPost])
in
    response

未经测试(因为我没有帐户或API凭据)。