Google Analytics数据上传Mime类型错误

时间:2013-03-25 13:34:11

标签: php google-analytics

以下是我在数据上传程序中使用的代码。

if(isset($_POST['upload'])){
try {
$dailyUploadsFile = $analytics->management_dailyUploads->upload(
$_REQUEST['accountId'],             // your accountID
$_REQUEST['webPropertyId'],         // your web property ID
$_REQUEST['customDataSourceId'],    // your custom data source UID
$_REQUEST['datepicker'],            // date
$_REQUEST['appendNumber'],          // append number
'cost',                             // type of data
array(
  "reset" => $_REQUEST['reset'],
  "data" => file_get_contents_curl($_REQUEST['csvFile']),
  "mimeType" => 'application/octet-stream',
  "uploadType" => 'media'));
} catch (Exception $e) {
die('An error occured: ' . $e->getMessage()."\n");
}
}

这是我点击提交时收到的错误:

发生错误:调用POST https://www.googleapis.com/upload/analytics/v3/management/accounts/34620205/webproperties/UA-34620205-1/customDataSources/P4Zlk69kSCOtVVIu7iFjqw/dailyUploads/2013-03-09/uploads?appendNumber=1&type=cost&reset=true&uploadType=media&key=AIzaSyDzvHpTNC_CKAnpyfnc1Vjwl_joE5hgBhc时出错:(400)不支持媒体类型'application / x-www-form-urlencoded'。有效的媒体类型:[application / octet-stream]

请帮忙。

1 个答案:

答案 0 :(得分:0)

不推荐使用Analytics API的dailyUploads resource。建议您使用uploads resource

以下示例适用于php,因为您拥有授权的分析服务对象。

$analytics->management_uploads->uploadData(
    '123456',
    'UA-123456-1',
    '122333444455555',
    array('data' => file_get_contents('example.csv'),
          'mimeType' => 'application/octet-stream',
          'uploadType' => 'media'));