google / google-api-javascript-client

Google APIs Client Library for browser JavaScript, aka gapi.
Apache License 2.0
3.22k stars 1.06k forks source link

Google analytics api 401 error Login required #190

Open anyatran opened 9 years ago

anyatran commented 9 years ago

I wanted to get data from my google analytics without OAuth and im getting 401 error. Currently i'm passing only API Key (I can't find Simple API Key since the design has changed). Here's my code:

function init() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(gapi.client.load('analytics', 'v3', makeApiCall), 1);
  }

function makeApiCall() {
    gapi.client.analytics.data.ga.get({
      'ids': document.getElementById('table-id').value,
      'start-date': document.getElementById('start-date').value,
      'end-date': document.getElementById('end-date').value,
      'metrics': 'ga:visits'
    }).execute(handleCoreReportingResults);
  }

 function handleCoreReportingResults(results) {
    if (!results.code) {
      console.log('Query Success');

    } else {
      console.log('There was an error: ' + results.message);
    }
}

I also tried to create a Service account but I'm not sure how can I use it here

cj3wilso commented 9 years ago

I'd like to know this too! Since things changed I'm unsure how to grab my own analytics data now.

vhatuncev commented 9 years ago

Check examples https://ga-dev-tools.appspot.com/embed-api/ it might be helpful for you

TMSCH commented 7 years ago

Hi @anyatran, sorry for the very late reply, have you figured out a solution? A 401 error means that you are not authorized to access the resource. I would assume that you do have to authenticate to access your Google Analytics.