Josh.Junghans
Josh.Junghans

Reputation: 11

Google Analytics API Javascript: 403 Forbidden Error

For the love of Bob, someone please help me out...

I'm trying to use the Google Analytics API (Javascript Library) to get some Analytics info. I've registered the my app and set up the oauth2 stuff. I can return the access token jsut fine, but when i try to send a request to actually grab Analytics info, it returns a 403 forbidden error. Here's my code:

function auth() {
    var config = {
        'client_id': '[my_client_id]',
        'scope': 'https://www.googleapis.com/auth/analytics.readonly'
    };
    gapi.auth.authorize(config, function() {
        var retObj = gapi.auth.getToken();
        makeRequest(retObj.access_token);
    });
}

function makeRequest(accessToken) {
    var restRequest = gapi.client.request({
        'path': '/analytics/v3/data/ga',
        'params': {
            'access_token': accessToken,
            'ids': 'ga:[table_number]',
            'metrics': 'ga:pageviews,ga:uniquePageviews',
            'start-date': '2011-11-01',
            'end-date' : '2011-12-01'               
        }
    });
    restRequest.execute(function(resp) { console.log(resp); });
}

The auth() function is executed via a button click and like I said, getting the access token is not the issue. It's when I execute the makeRequest function that I get the 403 error. Anyone have any clue as to what the deal is here?

Thanks to anyone who answers in advance!!

Upvotes: 1

Views: 3304

Answers (2)

s15199d
s15199d

Reputation: 7707

In my case I was getting 403 Forbidden because in my browser I was logged into Google with an account that didn't have permission to the GA profile I was trying to access. Before discovering that issue, I was having trouble with the tableID for which Aksival posted the solution above.

Here's my working code for your reference:

<script type="text/javascript">
    //GET THESE HERE https://code.google.com/apis/console/
    var clientId = 'YOURCLIENTIDHERE';
    var apiKey = 'YOURAPIKEYHERE';

    //GET THIS HERE http://code.google.com/apis/analytics/docs/gdata/v3/gdataAuthorization.html
    var scopes = 'https://www.googleapis.com/auth/analytics.readonly';

    //INITIALIZE
    function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(checkAuth,1);
    }

    function checkAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
    }

    function handleAuthResult(authResult) {
        if (authResult) {
            makeApiCall();
        } else {            
            requestAuth();
        }
    }

    function requestAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
    }

    function makeApiCall() {
        gapi.client.load('analytics', 'v3', function() {
            var request = gapi.client.analytics.data.ga.get({
                'ids':'ga:YOURTABLEIDHERE', 'start-date':'2012-01-01', 'end-date':'2012-02-01', 'metrics':'ga:visits', 'metrics':'ga:visits', 'start-index':1, 'max-results':1000
            });
            request.execute(function(resp) { console.log(resp.totalsForAllResults); });
        });
    }
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Upvotes: 2

claviska
claviska

Reputation: 12480

I had the same issue. Turns out I was passing in the wrong [table_number].

You need to query

accounts/[account-id]/webproperties/[webproperties-id]/profiles

and use the 'id' field of the appropriate property. (I was using the internalWebPropertyId from the webproperties query at first, which is why it was failing.)

Works like a charm now.

Upvotes: 1

Related Questions