【问题标题】:google analytics reporting api error谷歌分析报告api错误
【发布时间】:2016-10-24 06:16:22
【问题描述】:

我正在尝试发送版本 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."
          }
        ]
      }
    ]

我做错了什么?

【问题讨论】:

  • 您使用的是什么内容类型?
  • 内容类型:应用程序/json; charset=UTF-8
  • 我不使用 Content-Type:application/x-www-form-urlencoded; charset=UTF-8

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


【解决方案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"
      }
   ]
}

【讨论】:

  • 你能用ajax写代码吗?我试试跑到控制台
  • 不,对不起,我没有 ajax 的力量。只需将您的内容类型更改为“应用程序/Json”并试一试。它应该与标头和 URL 中的访问令牌一起使用。
  • 即时消息,但现在我收到此错误“消息”:“收到无效的 JSON 有效负载。意外的令牌。\nreportRequests%5B0%5\n^”,
  • 听起来你在某个地方有一些无效的字符。我通过 Json 美化器运行了你提出的内容,Json 是有效的,看起来和我的一样。里面肯定藏着别的东西。
【解决方案2】:

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

$.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. content-type 设置为 'application/json'
  2. 使用JSON.stringify()将对象转换成API服务器可以解析的字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2012-03-14
    • 1970-01-01
    相关资源
    最近更新 更多