简体   繁体   English

Google AnalyticsAPI Javascript:403禁止错误

[英]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. 我正在尝试使用Google AnalyticsAPI(Javascript库)来获取一些Google Analytics信息。 I've registered the my app and set up the oauth2 stuff. 我已经注册了我的应用程序并设置了oauth2内容。 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. 我可以很好地返回访问令牌jsut,但是当我尝试发送请求以实际获取Google Analytics(分析)信息时,它将返回403禁止错误。 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. auth()函数通过按钮点击执行,就像我说的那样,获取访问令牌不是问题。 It's when I execute the makeRequest function that I get the 403 error. 当我执行makeRequest函数时,我得到403错误。 Anyone have any clue as to what the deal is here? 有人对这笔交易有什么线索吗?

Thanks to anyone who answers in advance!! 感谢任何提前回答的人!

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. 在我的情况下,我得到了403 Forbidden,因为在我的浏览器中,我使用的帐户登录了Google,该帐户未获得我尝试访问的GA个人资料的权限。 Before discovering that issue, I was having trouble with the tableID for which Aksival posted the solution above. 在发现该问题之前,我遇到了Aksival发布上述解决方案的tableID问题。

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>

I had the same issue. 我遇到过同样的问题。 Turns out I was passing in the wrong [table_number]. 原来我传错了[table_number]。

You need to query 您需要查询

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

and use the 'id' field of the appropriate property. 并使用适当属性的“id”字段。 (I was using the internalWebPropertyId from the webproperties query at first, which is why it was failing.) (起初我是从webproperties查询中使用internalWebPropertyId的,这就是失败的原因。)

Works like a charm now. 现在就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM