谷歌分析报告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."
          }
        ]
      }
    ]

我做错了什么?

我刚刚发送了这个,这是与您的要求相同的日期,尺寸和指标。工作很好。我能看到的唯一区别是,我将访问令牌附加到URI的末尾,并且我只发送'application/Json''application/json; charset=UTF-8'似乎也能起作用。

我认为这是在文档中,我会通知开发人员,让他们把它添加到某个地方。

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"
      }
   ]
}

这是一个重写的请求。有两件事需要修复:

$.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服务器可以解析的字符串

相关内容

  • 没有找到相关文章

最新更新