Getting CORS Error when using Google Business API

272 Views Asked by At

I am using google business API and trying to get location list.

I am going trough their documentation and using project from the link below as basis

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

Using that project I am able to successfully retrieve accounts list.

The problem occures when I try to retrieve locations list using accountID. Here is a link for their documentation https://developers.google.com/my-business/content/manage-locations

Acording to documentation, to get location list for specific account, I should use following request

GET

https://mybusinessbusinessinformation.googleapis.com/v1/{accountId}/locations

Authorization: Bearer <access_token>

This is the code snippet, that I added to their sample project

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);
        }
    });
}

When I do this request, it gives "CORS error". The error in the console is following

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

From the error message it seems that server does no accept requests from localhost:8001, but if I do request to different endpoint it will return result.

For example if I change utl from

https://mybusinessbusinessinformation.googleapis.com/v1/{accID}/locations

to

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

With the second url it will return successfull result.

I am confused why it is allowing requests to one endpoint and blocks requests to other endpoint. Can anyone help with this problem?

1

There are 1 best solutions below

0
On

Have you tried making direct API requests via the Google Developers OAuth 2.0 Playground?
I tried to reproduce your error but got the expected 404 Not Found response for the unsupported endpoint pattern.