I have this code made by Google and I want to make server-side authentication, but the access token I get on https://developers.google.com/oauthplayground/ doesn't seem to work. What am I doing wrong?
When I use client-side authentication it works, but I need to show the charts without login. And of course, I deleted the access token just to post.
<script>
gapi.analytics.ready(function() {
// Step 3: Authorize the user.
var CLIENT_ID = 'Insert your client ID here';
gapi.analytics.auth.authorize({
serverAuth: {
access_token: ' '
}
});
// Step 4: Create the view selector.
var viewSelector = new gapi.analytics.ViewSelector({
container: 'view-selector'
});
// Step 5: Create the timeline chart.
var timeline = new gapi.analytics.googleCharts.DataChart({
reportType: 'ga',
query: {
'dimensions': 'ga:date',
'metrics': 'ga:sessions',
'start-date': '30daysAgo',
'end-date': 'yesterday',
},
chart: {
type: 'LINE',
container: 'timeline'
}
});
// Step 6: Hook up the components to work together.
gapi.analytics.auth.on('success', function(response) {
viewSelector.execute();
});
viewSelector.on('change', function(ids) {
var newIds = {
query: {
ids: ids
}
}
timeline.set(newIds).execute();
});
});
</script>
When I use client-side authentication it works, but I need to show the charts without login. And of course, I deleted the access token just to post.
The data you are trying to access is private user data in order to access private user data you need to be authorized by the user. The only way to do that is to request access of the user. To do that we use Oauth2.
Oauth2 access tokens expire after an hour so what you are trying to do is not going to work. The token you have created though google oauth playground is an access token.
You need to create web browser credentials on google cloud console. make sure to set the JavaScript origin for your local server as well, for example: http://localhost:8080.
This video will walk you though how to create the credentials. web application redetinals