Cần có uỷ quyền
Tải dữ liệu lên cho một nguồn dữ liệu tuỳ chỉnh. Xem ví dụ.
Phương thức này hỗ trợ URI /upload và chấp nhận nội dung nghe nhìn đã tải lên có các đặc điểm sau:
- Kích thước tệp tối đa: 1 GB
- Các loại MIME nội dung đa phương tiện được chấp nhận:
application/octet-stream
Ngoài các thông số chuẩn, phương thức này còn hỗ trợ các thông số được liệt kê trong bảng thông số.
Yêu cầu
Yêu cầu HTTP
POST https://www.googleapis.com/upload/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/customDataSources/customDataSourceId/uploads
Tham số
Tên thông số | Giá trị | Nội dung mô tả |
---|---|---|
Tham số đường dẫn | ||
accountId |
string |
Mã tài khoản được liên kết với tệp tải lên. |
customDataSourceId |
string |
Mã nguồn dữ liệu tuỳ chỉnh chứa dữ liệu đang được tải lên. |
webPropertyId |
string |
Chuỗi UA thuộc tính web được liên kết với tệp tải lên. |
Tham số truy vấn bắt buộc | ||
uploadType |
string |
Loại yêu cầu tải lên URI /upload.
Các giá trị được chấp nhận là:
|
Ủy quyền
Yêu cầu này cần được uỷ quyền với ít nhất một trong các phạm vi sau (đọc thêm về quy trình xác thực và uỷ quyền).
Phạm vi |
---|
https://www.googleapis.com/auth/analytics |
https://www.googleapis.com/auth/analytics.edit |
Nội dung yêu cầu
Không cung cấp nội dung yêu cầu bằng phương thức này.
Phản hồi
Nếu thành công, phương thức này sẽ trả về một Tài nguyên tải lên trong nội dung phản hồi.
Ví dụ
Lưu ý: Các đoạn mã mẫu của phương thức này không phải là ví dụ cho mọi ngôn ngữ lập trình được hỗ trợ (xem trang thông tin về các thư viện dùng cho ứng dụng để biết danh sách các ngôn ngữ được hỗ trợ).
Java
Sử dụng thư viện ứng dụng 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()); }
1.199
Sử dụng thư viện ứng dụng 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
Sử dụng thư viện ứng dụng 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
Sử dụng thư viện ứng dụng 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. }); } }