Hi,
I am trying to access some metrics through the Google Analytics API through JQuery $getJSON.
My initial authorisation request is as follows,
which requests permission for 2 scopes - I am using this to request
an access_token which seems to work correctly.
<a href='https://accounts.google.com/o/oauth2/auth?state=%2Fprofile
&redirect_uri=https%3A%2F%2Flocalhost%2FHelloAnalytics%2FOAuth2Response.html
&response_type=token
&client_id=455xxxxxx.apps.googleusercontent.com
&approval_prompt=auto
&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile
+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly'>
Login</a>
The response is parsed to get the access_token. Which is added as a param for the JSON request.
<!DOCTYPE>
<html>
<head>
<title>OAuth2 Handle Response</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
<script type="text/javascript" language="javascript">
var tokenValue;
var gurl = "https://www.googleapis.com/analytics/v3/data/ga?callback=?";
function showReportData(data) {
if (data == null)
alert("data is null");
else {
alert("data is not null");
var JSONText = JSON.stringify(data);
document.getElementById('output').innerHTML = JSONText;
}
};
function parseResponse() {
var params = {}, queryString = location.hash.substring(1), regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
if (m[1] == 'access_token'){
tokenValue = m[2];
}
}
var parms = {
'access_token': tokenValue,
'ids': "ga:xxxxx28",
'start-date': "2012-03-20",
'end-date': "2012-03-30",
'metrics': "ga:visits",
'dimensions': "ga:source,ga:keyword",
'max-results': 30
}
$.getJSON(gurl, parms, showReportData);
};
</script>
<body onload="javascript:parseResponse();">
<div id="output"></div>
</body>
</html>
However I am getting an error: {"error":{"errors":[{"message":"Forbidden"}],"code":403,"message":"Forbidden"}}
I also tried this in the OAuth 2.0 playground. The request was a
follows
https://www.googleapis.com/analytics/v3/data/ga?
&access_token=ya29.AHES6ZT-_lEKzrB6YJzYbMPZ2i0u0yIjkQI9FskTOy1p7K4
I get the same result.
But I am not sure what settings to use for the playground.
Is there anything else I can try next to debug this or get it working?
Thanks,
Sinead