I’m building a dashboard that will pull in data from GA. To do this I need to get an access token via Google Accounts and then I need to query GA with headers including this authenticated token.
Heres my code:
$.ajax({
async: false,
url: "https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email=EMAIL&Passwd=PASSWORD&service=analytics",
success : function(data) {
var auth = data.substring(data.indexOf("Auth=") + 5);
console.log(auth);
$.ajax({
type: 'get',
headers: {
"Content-type": "application/x-www-form-urlencoded",
"Authorization": "GoogleLogin Auth=" + auth,
"GData-Version": "2",
},
url: "https://www.google.com/analytics/feeds/data?ids=ga:PROFILE&start-date=2011-08-16&end-date=2012-06-16&max-results=10000",
});
}
});
Unfortunately this method isn’t working. If anyone could give me pointers of how to get this working or better ways of authenticating such requests this would be much appreciated.
It’s not working because ClientLogin requires a
postrequest not aget. It’s also been deprecated.You also seem to have violated the same-origin policy for ajax requests.
Use the OAuth 2 version with a refresh token. You can use one of the client libraries on your site and post ajax requests to your own page.