2016-10-04 94 views
0

我是Google Analytics API的“新手”,我试图让“Hello Analytics”示例正常工作。Hello Analytics示例生成403错误

我跟着their steps 来创建测试网页,但我没有结果。

当我进入我的网页时,我看到标题,“登录”按钮和下面显示结果的文本区域。当我按“登录”时,这似乎工作;该按钮变为“已登录”。但是,没有结果显示。

我使用的是FireFox,所以我右键单击文本区域并选择“检查元素”;这表明此错误消息:

对象{结果:对象,身体: “{ ”错误“:{ ”代码“:403,...”, 头:对象,状态:403,状态文本: “紫禁城” }

我认为这是一个权限错误,但不知道如何纠正它。

这里是我的文件HelloAnalytics.html:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Hello Analytics Reporting API V4</title> 
    <meta name="google-signin-client_id" content="959582115031-ardmn5vsir7kbcp0dme4d4n1p45bd649.apps.googleusercontent.com"> 
    <meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly"> 
</head> 
<body> 

<h1>Hello Analytics Reporting API V4</h1> 

<p> 
    <!-- The Sign-in button. This will run `queryReports()` on success. --> 
    <div class="g-signin2" data-onsuccess="queryReports"></div> 
</p> 

<!-- The API response will be printed here. --> 
<textarea cols="80" rows="20" id="query-output"></textarea> 

<script> 
    // Replace with your view ID. 
    var VIEW_ID = '92320289'; 

    // Query the API and print the results to the page. 
    function queryReports() { 
    gapi.client.request({ 
     path: '/v4/reports:batchGet', 
     root: 'https://analyticsreporting.googleapis.com/', 
     method: 'POST', 
     body: { 
     reportRequests: [ 
      { 
      viewId: VIEW_ID, 
      dateRanges: [ 
       { 
       startDate: '7daysAgo', 
       endDate: 'today' 
       } 
      ], 
      metrics: [ 
       { 
       expression: 'ga:sessions' 
       } 
      ] 
      } 
     ] 
     } 
    }).then(displayResults, console.error.bind(console)); 
    } 

    function displayResults(response) { 
    var formattedJson = JSON.stringify(response.result, null, 2); 
    document.getElementById('query-output').value = formattedJson; 
    } 
</script> 

<!-- Load the JavaScript API client and Sign-in library. --> 
<script src="https://apis.google.com/js/client:platform.js"></script> 

</body> 
</html> 

我需要了解如何使用谷歌Analytics(分析)API做简单的查询。

+0

你是在本地运行还是在服务器上运行? – sideroxylon

+0

如何在谷歌分析API中进行分页? –

回答

1

最后得到它的工作!

我试图使用API​​来获取Google样本网站(“Google Merchandise Store”)的数据。我试图考虑许可错误的可能原因,并且我想到Google可能不允许该网站的Google Analytics API调用。

因此,我通过设置自己的测试网站,并使用Google的Javascript跟踪代码,然后定义了一个新项目,为其启用API,创建凭据(客户端ID)以及获取视图ID为我的网站。通过我的HelloAnalytics.html页面中的新客户端ID和视图ID,它可以很好地工作。

所以我的猜测是正确的:Google不允许API调用获取其“Google Merchandise Store”示例网站的数据。他们真的应该在their "quickstart" page上提到这个,因为一个“新手”(像我)不会知道这一点!

我以为我应该发布我的解决方案,因为我确信有人会在未来遇到这个问题。

仅供参考 - 我有HelloAnalytics.html运行在Web服务器上。我正在使用我的ISP提供的免费网络空间。

+0

是的,您无法对Google Merchandise Store进行API调用,该调用位于本帮助文章的底部https://support.google.com/analytics/answer/6367342?hl=zh-CN – MarkeD

+0

哦,是的,他们的确在说那里;从未看过那​​个页面。谢谢 –

+0

在快速入门指南中,他们也在最下方注明“注意:要成功运行此示例,您至少需要拥有一个Google Analytics媒体资源和视图。”人们可以阅读的是,你必须拥有属于你的财产和观点。 – Matt