2016-10-24 141 views
1

我試圖爲第4版API 發送請求我做這個簡單的要求谷歌分析報告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." 
      } 
     ] 
     } 
    ] 

我做錯了什麼?

+0

您使用的是什麼內容類型? – DaImTo

+0

content-type:application/json; charset = UTF-8 –

+0

沒有使用 內容類型:application/x-www-form-urlencoded; charset = UTF-8 –

回答

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

可以用阿賈克斯書寫代碼嗎?我試着跑進控制檯 –

+0

不,我沒有阿賈克斯的力量對不起。只需將您的內容類型更改爲'application/Json'並嘗試一下。它應該與標題以及URL中的訪問令牌一起工作。 – DaImTo

+0

即時通訊,但現在我有這個錯誤 「消息」:「收到無效的JSON負載。意外的令牌。\ nreportRequests%5B0%5 \ n ^」, –

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. 設置內容類型'應用/ JSON'
  2. 使用JSON.stringify()到對象轉換成一個字符串,它可以由API服務器解析