How to get realtime reporting api data after signin using google?

286 Views Asked by At

To access the real time reporting api, i used the authSample.html file and which uses Oauth 2.0 client_id for authorization and api request.

But, Google real time reporting api said

"Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you."

What does it mean by Google Sign-In part ?

My application has already Google sign-in setup, so does that mean i must not use auth.html file plus Oauth2.0 credentials ?

I already googled but not finding any satisfactory answer.

1

There are 1 best solutions below

2
On

I'm not sure what technology you are using but this is a great reference:

https://developers.google.com/api-client-library/

As is this:

https://github.com/google/google-api-javascript-client/blob/master/samples/authSample.html

It could be that you copied two or more samples into the same file and they are competing or that the person accessing the reports does not have permissions.

https://developers.google.com/analytics/devguides/reporting/realtime/v3/authorization

From the page above:

Troubleshooting

Your authorization fails in these situations:

You will get a 401 status code if your access_token has expired or if you are using the wrong scope for the API. You will get a 403 status code if the authorized user does not have access to the view (profile). Make sure you are authorized with the correct user and that they indeed have the view (profile) you have selected. OAuth 2.0 playground

This tool allows you to go through the entire authorization flow through a web interface. The tool also displays all the HTTP request headers required for making an authorized query. If you can't get authorization to work in your own application, you should try to get it working through the OAuth 2.0 playground. Then you can compare the HTTP headers and request from the playground to what your application is sending to Google Analytics. This check is a simple way to ensure you format your requests properly.

Invalid grant

When you try to use a refresh token, the following returns you an invalid_grant error:

Your server's clock is not in sync with network time protocol - NTP. The refresh token limit has been exceeded. Applications can request multiple refresh tokens to access a single Google Analytics account.

For example, if a user wants to install an application on multiple machines and access the same Google Analytics account, then a separate token would be required for each machine. When the number of refresh tokens exceeds the limit, older tokens become invalid. If the application attempts to use an invalidated refresh token, an invalid_grant error response is returned.

The limit for each unique pair of OAuth 2.0 client and Google Analytics account is 25 refresh tokens. If the application continues to request refresh tokens for the same Client/Account pair, once the 26th token is issued, the 1st refresh token that was previously issued will become invalid. The 27th requested refresh token would invalidate the 2nd previously issued token and so on.

Note: The limit of 25 refresh tokens is subject to change.

Samples: https://developers.google.com/analytics/devguides/reporting/realtime/v3/libraries

In the past when I've built these reports I've learned you can't rely on the samples to be working code as environments can and will vary and you so you will need to echo/print errors codes to see what the actual errors are. If you can post specific web/server errors, we can go from there to troubleshoot.

I hope all of this helps somewhat.