Get google analytics Api response

I am trying to return GA API response but having a authentication error, which I don't know why. What I have done so far is right below.

<meta name="google-signin-client_id" content="MY_CLIENT_ID">
<meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly">

<script src="https://apis.google.com/js/platform.js?onload=init" async defer></script>
<script src="https://apis.google.com/js/client:platform.js"></script>

then gapi response method.

queryReports() {
    gapi.client.request({
        path: '/v4/reports:batchGet',
        root: 'https://analyticsreporting.googleapis.com/',
        method: 'POST',
        body: {
            reportRequests: [{
                viewId: "82708385",
                dateRanges: [{
                        startDate: '7daysAgo',
                        endDate: 'today'
                    }],
                metrics: [{
                        expression: 'ga:sessions'
                }]
            }]
        }
    })
    .then(res => {
        console.log(res.result)
    })
    .catch(err => {
        console.log(err)
    })
},

But in the end it's catching an AUTH error. The thing is. I already create a OAuth 2.0 Client ID credentials in console.developers.google.com/apis/credentials? and using that client_id in meta.

error is:

"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.

It's look like I am missing something here? How can I solve this issue.

1 answer

  • answered 2020-02-10 08:31 DaImTo

    Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.

    You appear to have set your client id. The most next logical thing to consider would be wrong is that there is something wrong with your credentials. Either your you copied the client id incorrectly or you created the wrong type.

    I believe this is a setup issue with your client. If you check the tutorial Hello analytics reporting java script.

    enter image description here

    As you can see you need to create type Web Application. According to your comments you have created type other this will not work as that is used for installed applications.