使用Google Business API时获取CORS错误



我正在使用google商务API并试图获取位置列表。

我正在浏览他们的文档,并使用下面链接中的项目作为基础

https://developers.google.com/my-business/content/implement-oauth

使用该项目,我能够成功检索帐户列表。

当我尝试使用accountID检索位置列表时,会出现此问题。这是他们文档的链接https://developers.google.com/my-business/content/manage-locations

根据文件,要获得特定账户的位置列表,我应该使用以下请求

GET
https://mybusinessbusinessinformation.googleapis.com/v1/{accountId}/locations
Authorization: Bearer <access_token>

这是我添加到他们的示例项目中的代码片段

function retrieveGoogleMyBusinessLocations(accessToken) {
$.ajax({
type: 'GET',
url: 'https://mybusinessbusinessinformation.googleapis.com/v1/{accID}/locations',
headers: {
'Authorization' : 'Bearer ' + accessToken
},
success: function(returnedData) {
var e = document.createElement("pre")
e.innerHTML = JSON.stringify(returnedData, undefined, 2);
document.body.appendChild(e);
}
});
}

当我提出这个请求时;CORS错误";。控制台中的错误跟随

Access to XMLHttpRequest at 'https://mybusinessbusinessinformation.googleapis.com/v1/xxx/locations' from origin 'http://localhost:8001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
GET https://mybusinessbusinessinformation.googleapis.com/v1/xxx/locations net::ERR_FAILED

从错误消息来看,服务器似乎不接受来自localhost:8001的请求,但如果我向不同的端点请求,它将返回结果。

例如,如果我从更改utl

https://mybusinessbusinessinformation.googleapis.com/v1/{accID}/位置

https://mybusinessbusinessinformation.googleapis.com/v1/accounts/{accID}/位置?readMask=类别

使用第二个url,它将返回成功的结果。

我很困惑为什么它允许请求到一个端点,而阻止请求到另一个端点。有人能帮我解决这个问题吗?

您是否尝试过通过Google Developers OAuth 2.0 Playground直接发出API请求?
我试图重现您的错误,但对于不受支持的端点模式,得到了预期的404 Not Found响应。

最新更新