Ajax 谷歌分析报告api错误

Ajax 谷歌分析报告api错误,ajax,google-api,google-analytics-api,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", "dat

我正在尝试发送对版本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'似乎也能工作

实际上,我认为这是在文档中,我将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"
      }
   ]
}
URl:'https://analyticsreporting.googleapis.com/v4/reports:batchGet?access_token='
ContentType='application/Json'
{  
“报告请求”:[
{  
“视图ID”:“ga:78110423”,
“日期范围”:[
{  
“起始日期”:“2015-06-15”,
“结束日期”:“2015-06-15”
}
],
“尺寸”:[
{  
“名称”:“ga:浏览器”
}
],
“指标”:[
{  
“表达式”:“ga:会话”
}
],
“pageToken”:“0”,
“页面大小”:“1000”,
“includeEmptyRows”:“true”,
“hideTotals”:“真的”,
“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);
    }
});
  • 内容类型设置为'application/json'
  • 使用JSON.stringify()将对象转换为可由API服务器解析的字符串

  • 您使用的内容类型是什么?内容类型:application/json;charset=UTF-8无im使用内容类型:应用程序/x-www-form-urlencoded;charset=UTF-8你能用ajax写代码吗?我试着运行到控制台我没有ajax的能力对不起。只需将您的内容类型更改为“application/Json”,并尝试一下。它应该与标头以及URL.im通道中的访问令牌一起工作,但现在我收到了以下错误“消息”:“接收到无效的JSON负载。意外的令牌。\nreportRequests%5B0%5\n^”,听起来您在某个地方有一些无效的字符。我用Json美化器运行了你摆出的姿势,Json是有效的,看起来和我的一样。里面一定藏着别的东西。