1

I want to fetch the data(Pageview, Session, Users and all) from Google analytics and show it on another website but my problem is that I don't know how to get access token using JavaScript.

I referred some Google docs for getting access token Query Explorer, OAuth 2.0 Playground but this access token is expired in 60 minute and I want to save this access token for future use(long live access token). Anyone know about how to get access token using js or how to get refresh token for using future use.

Whenever I used expired access token and fire the API so I got this error:

{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}  

And my Google analytics API which is generated by Query Explorer included access_token.

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A123456&start-date=2016-10-21&end-date=2016-11-20&metrics=ga%3Asessions%2Cga%3Apageviews&dimensions=ga%3AlandingPagePath&start-index=3&max-results=1&access_token=za29.aaaaaaaaabbbbbbbbbccccccccdddddddmmvermrm
2
  • You need to read the OAuth documentation developers.google.com/identity/protocols/OAuth2 Commented Nov 25, 2016 at 10:50
  • Sorry, I already read it several times but I'm not getting :( how to get access_token? @Liam Commented Nov 25, 2016 at 10:55

1 Answer 1

1

Access tokens are only good for 60 minutes.

What you are looking for is a Refresh Token. Refresh tokens are used are good as long as they are valid. They can be used to request a new access token from the server at anytime.

Answer: It is not possible to extend the time that an access token is good for. It is not possible to get a refresh token with JavaScript. This is probably for security reasons.

Option: Switch to a server sided programing language if you want to get a refresh token. If this is your own personal account and you wont be accessing user data I would recommend you looking into service accounts instead. You will need to use a server sided language for service accounts as well.

Sign up to request clarification or add additional context in comments.

2 Comments

okay, Google analytics account is mine. I want to show the data on another website for different-2 users using google analytics API so what is the soluttion for that? @DalmTo
Assuming you have access to this other website. I would go with service accounts. its easer and you don't have to go though the trouble of dealing with Oauth2. You need a server sided programing language tough like php or python. you can try reading my post about it should explain it a little more then googles documentation daimto.com/google-developer-console-service-account

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.