0

我正在使用谷歌业务 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}/locations

https://mybusinessbusinessinformation.googleapis.com/v1/accounts/{accID}/locations?readMask=categories

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

我很困惑为什么它允许对一个端点的请求并阻止对另一个端点的请求。任何人都可以帮助解决这个问题吗?

4

1 回答 1

0

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

于 2022-02-04T18:22:47.993 回答