1

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

这里有一些额外的信息:

Power Query - 参考

Google 网站管理员 API - 参考

PowerBi 社区

格式日期不同:

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

获得 400 并在 Gooogle-Api 概述中显示为 400 调用

任何想法有什么问题?

谢谢

4

1 回答 1

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. POST必须使用 HTTP方法
  2. 网址必须有效
    • 您还没有提供您的实际 URL,因此您必须自己验证它。我会去掉你的尾随?url因为你不包括查询字符串——即使你是,你也应该将它们传递给记录的Query字段,options而不是自己构建查询字符串)。
  3. 标题 ( Authorization, Accept, Content-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 凭据)。

于 2019-04-13T13:56:34.233 回答