【问题标题】:Google Analytics API Javascript: 403 Forbidden ErrorGoogle Analytics API Javascript:403 禁止错误
【发布时间】:2012-01-17 23:49:55
【问题描述】:

看在 Bob 的份上,请有人帮帮我...

我正在尝试使用 Google Analytics API(Javascript 库)来获取一些分析信息。我已经注册了我的应用程序并设置了 oauth2 的东西。我可以很好地返回访问令牌,但是当我尝试发送请求以实际获取分析信息时,它会返回 403 禁止错误。这是我的代码:

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); });
}

auth() 函数是通过单击按钮执行的,就像我说的,获取访问令牌不是问题。当我执行 makeRequest 函数时,我得到了 403 错误。有人知道这里的交易是什么吗?

感谢任何提前回答的人!

【问题讨论】:

    标签: javascript api google-analytics


    【解决方案1】:

    在我的情况下,我收到了 403 Forbidden,因为在我的浏览器中,我使用一个无权访问我试图访问的 GA 个人资料的帐户登录了 Google。在发现这个问题之前,我遇到了 Aksival 为其发布上述解决方案的 tableID 的问题。

    这是我的工作代码供您参考:

    <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>
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。原来我传入了错误的 [table_number]。

      您需要查询

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

      并使用相应属性的“id”字段。 (我一开始使用的是 webproperties 查询中的 internalWebPropertyId,这就是它失败的原因。)

      现在就像一个魅力。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-07
        • 2015-07-31
        • 1970-01-01
        • 2013-05-12
        • 2013-04-05
        • 1970-01-01
        相关资源
        最近更新 更多