需要授權
上傳自訂資料來源的資料。 參閱範例。
這個方法支援 /upload URI,可接受具備下列特性的上傳媒體:
- 檔案大小上限:1 GB
- 接受的媒體 MIME 類型:
application/octet-stream
除了標準參數以外,這個方法也支援參數表格中列出的參數。
要求
HTTP 要求
POST https://www.googleapis.com/upload/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/customDataSources/customDataSourceId/uploads
參數
參數名稱 | 值 | 說明 |
---|---|---|
路徑參數 | ||
accountId |
string |
與上傳作業相關聯的帳戶 ID。 |
customDataSourceId |
string |
上傳資料所屬的自訂資料來源 ID。 |
webPropertyId |
string |
與上傳作業相關聯的網站資源 UA- 字串。 |
必要的查詢參數 | ||
uploadType |
string |
/upload URI 的上傳要求類型。可接受的值如下: |
授權
這項要求需要至少下列其中一個範圍的授權 (進一步瞭解驗證和授權)。
內容範圍 |
---|
https://www.googleapis.com/auth/analytics |
https://www.googleapis.com/auth/analytics.edit |
要求主體
請勿使用這個方法提供要求主體。
回應
如果成功的話,這個方法會在回應主體中傳回上傳資源。
示例
注意:這個方法適用的程式語言眾多,我們只在此提供部分程式碼範例,完整的支援語言清單請參閱用戶端程式庫頁面。
Java
使用 Java 用戶端程式庫。
/* * Note: This code assumes you have an authorized Analytics service object. * See the Data Import Developer Guide for details. */ // This request uploads a file for the authorized user. File file = new File("data.csv"); InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", new FileInputStream(file)); mediaContent.setLength(file.length()); try { analytics.management().uploads().uploadData("123456", "UA-123456-1", "122333444455555", mediaContent).execute(); } catch (GoogleJsonResponseException e) { System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage()); }
PHP
使用 PHP 用戶端程式庫。
/** * Note: This code assumes you have an authorized Analytics service object. * See the Data Import Developer Guide for details. */ /** * This request uploads a file to a custom data source. */ try { $analytics->management_uploads->uploadData( '123456', 'UA-123456-1', '122333444455555', array('data' => file_get_contents('example.csv'), 'mimeType' => 'application/octet-stream', 'uploadType' => 'media')); } catch (apiServiceException $e) { print 'There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage(); } catch (apiException $e) { print 'There was a general API error ' . $e->getCode() . ':' . $e->getMessage(); }
Python
使用 Python 用戶端程式庫。
# Note: This code assumes you have an authorized Analytics service object. # See the Data Import Developer Guide for details. # This request uploads a file custom_data.csv to a particular customDataSource. # Note that this example makes use of the MediaFileUpload Object from the # apiclient.http module. from apiclient.http import MediaFileUpload try: media = MediaFileUpload('custom_data.csv', mimetype='application/octet-stream', resumable=False) daily_upload = analytics.management().uploads().uploadData( accountId='123456', webPropertyId='UA-123456-1', customDataSourceId='9876654321', media_body=media).execute() except TypeError, error: # Handle errors in constructing a query. print 'There was an error in constructing your query : %s' % error except HttpError, error: # Handle API errors. print ('There was an API error : %s : %s' % (error.resp.status, error.resp.reason))
JavaScript
/* * Note: This code assumes you have an authorized Analytics client object. * See the Data Import Developer Guide for details. */ /* * This request uploads a file for the authorized user. */ function uploadData(fileData) { const boundary = '-------314159265358979323846'; const delimiter = "\r\n--" + boundary + "\r\n"; const close_delim = "\r\n--" + boundary + "--"; var contentType = 'application/octet-stream' var reader = new FileReader(); reader.readAsBinaryString(fileData); reader.onload = function(e) { var contentType = 'application/octet-stream'; var metadata = { 'title': fileData.name, 'mimeType': contentType }; var base64Data = btoa(reader.result); var multipartRequestBody = delimiter + 'Content-Type: application/json\r\n\r\n' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + contentType + '\r\n' + 'Content-Transfer-Encoding: base64\r\n' + '\r\n' + base64Data + close_delim; var request = gapi.client.request({ 'path': 'upload/analytics/v3/management/accounts/123456/webproperties/UA-123456-1/customDataSources/ABCDEFG123abcDEF123/uploads', 'method': 'POST', 'params': {'uploadType': 'multipart'}, 'headers': { 'Content-Type': 'multipart/mixed; boundary="' + boundary + '"' }, 'body': multipartRequestBody }); request.execute(function (response) { // Handle the response. }); } }