谷歌分析报告API错误

时间:2016-10-24 06:16:22

标签: ajax google-api google-analytics-api

我正在尝试发送第4版api的请求 我做这个简单的请求

$.ajax({
  url: 'https://analyticsreporting.googleapis.com/v4/reports:batchGet',
  headers: {
        "Authorization":"Bearer xxxx"
    },
  method:"POST",
  data:{
  "reportRequests":[
  {
    "viewId":"xxx",
    "dateRanges":[
      {
        "startDate":"2015-06-15",
        "endDate":"2015-06-30"
      }],
    "metrics":[
      {
        "expression":"ga:sessions"
      }],
    "dimensions": [
      {
        "name":"ga:browser"
      }]
    }]
},
  success: function(resp){
    alert(resp);
  }
});

但这是回归错误。

"details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"reportRequests[0][metrics][0][expression]\": Cannot bind query parameter. Field 'reportRequests[0][metrics][0][expression]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"reportRequests[0][dateRanges][0][endDate]\": Cannot bind query parameter. Field 'reportRequests[0][dateRanges][0][endDate]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"reportRequests[0][dimensions][0][name]\": Cannot bind query parameter. Field 'reportRequests[0][dimensions][0][name]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"reportRequests[0][dateRanges][0][startDate]\": Cannot bind query parameter. Field 'reportRequests[0][dateRanges][0][startDate]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"reportRequests[0][viewId]\": Cannot bind query parameter. Field 'reportRequests[0][viewId]' could not be found in request message."
          }
        ]
      }
    ]

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我刚刚发送了与您的请求相同的日期,维度和指标。工作良好。我能看到的唯一区别是我将访问令牌添加到URI的末尾,而我只发送​​'application/Json''application/json; charset=UTF-8'似乎也有效。

我实际上认为这是在文档中我将ping开发人员并要求他们将其添加到某个地方。

URl:    'https://analyticsreporting.googleapis.com/v4/reports:batchGet?access_token=<access_token>'
ContentType = 'application/Json'

{  
   "reportRequests":[  
      {  
         "viewId":"ga:78110423",
         "dateRanges":[  
            {  
               "startDate":"2015-06-15",
               "endDate":"2015-06-15"
            }
         ],
         "dimensions":[  
            {  
               "name":"ga:browser"
            }
         ],
         "metrics":[  
            {  
               "expression":"ga:sessions"
            }
         ],
         "pageToken":"0",
         "pageSize":"1000",
         "includeEmptyRows":"true",
         "hideTotals":"true",
         "hideValueRanges":"true"
      }
   ]
}

答案 1 :(得分:0)

这是一个有效的重写请求。有两件事需要解决:

$.ajax({
    url: 'https://analyticsreporting.googleapis.com/v4/reports:batchGet',
    headers: {
        "Authorization": "Bearer XXX"
    },
    method: "POST",
    data: JSON.stringify({
        "reportRequests": [{
            "viewId": "XXX",
            "dateRanges": [{
                "startDate": "2015-06-15",
                "endDate": "2015-06-30"
            }],
            "metrics": [{
                "expression": "ga:sessions"
            }],
            "dimensions": [{
                "name": "ga:browser"
            }]
        }]
    }),
    contentType: 'application/json',
    success: function(resp) {
        alert(resp);
    }
});
  1. 内容类型设置为'application / json'
  2. 使用 JSON.stringify()将对象转换为可由API服务器解析的字符串