1
votes

I'm try to send request for version 4 api I'm do this simple request

$.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);
  }
});

But it's return error.

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

What I'm do wrong?

2
what content type are you using?DaImTo
content-type:application/json; charset=UTF-8Иван Семенов
no im use Content-Type:application/x-www-form-urlencoded; charset=UTF-8Иван Семенов

2 Answers

1
votes

I just sent this which is the same dates and dimensions and metrics as your request. Works fine. Only difference I can see is that I tack the access token onto the end of the URI and that I am only sending 'application/Json'. 'application/json; charset=UTF-8' appears to work as well.

I actually thought this was in the documentation I will ping the Developers and ask them to add it someplace.

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

Here's a rewritten request that works. There were two things to fix:

$.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. set the content-type to 'application/json'
  2. use JSON.stringify() to transform the object into a string that can be parsed by the API server