我通过Power-Query(M)调用了google.webmasters.api,并成功配置了oath2,并首次成功调用了get&list。 现在,我尝试调用/ searchAnalytics / query吗?仅适用于Post。 这总是以400错误响应。查询或网址的格式无法正常工作。
还有一些其他信息:
Google Webmaster Api - Reference
格式日期不同:
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
答案 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
{}
所以主要的收获是:
POST
方法
Web.Contents
文档(https://docs.microsoft.com/en-us/powerquery-m/web-contents)建议在Content
记录中包含options
字段,以将请求从GET
更改为POST
。 / li>
?
中尾随url
的影响(因为您不包括查询字符串-即使您已经包含了查询字符串,也应将它们传递到的Query
字段中options
记录,而不是自己构建查询字符串。Authorization
,Accept
,Content-Type
)应该是有效/存在的。
Headers
记录的options
字段。这使您有机会查看/检查标题(以确保它们符合预期)。Json.FromValue
(https://docs.microsoft.com/en-us/powerquery-m/json-fromvalue)似乎是一种更好的方法。总而言之,您的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凭据)。