1
votes

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
please edit your question and include the full error message. catching an AUTH error. is not enough information on exactly which auth error you are getting.DaImTo
@Tay, may be the access token you're using is either expired or invalid.Vijaya Varma Lanke
@VijayaVarmaLanke the client library should handle that by requesting authorization again.DaImTo
@DaImTo I updated the question with error messageyvl
I would check your client id again go grab it again from the Google developer console. sounds like you have copied it incorrectly. Make sure its the correct type as well. Web application.DaImTo

1 Answers

0
votes

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.