iT邦幫忙

0

google drive api post上傳檔案失敗問題?求解

QQ 2019-08-04 16:17:072832 瀏覽

如題
按文件說的做
https://ithelp.ithome.com.tw/upload/images/20190804/20113281wTb4oAwD5s.jpg

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable
的網址 Headers的Authorization和Content-Type我都確定有設好
但是卻跳出 Parse Error
https://ithelp.ithome.com.tw/upload/images/20190804/20113281cveMUoeZZF.jpg
我不太了解到底是哪裡出了問題

我用文件說的第二個API[分段上傳]是正常的
Multipart upload: uploadType=multipart
第二個API只是不用設定Content-Type

而我出錯的API是文件中說的第三個API[可恢復上傳]
Resumable upload: uploadType=resumable

因為我希望如果傳送大檔案比如影片,萬一網路斷線他可以不要重新上傳
而是從中段處續傳,我目前還沒搞清楚這API有沒有我想要的這功能
但我現在用第三個API根本連檔案都傳不上去,剩下的根本就都不用談

可恢復上傳還有一個PUT方法
網址列參數傳送檔案ID,上傳時更新雲端裡的檔案我用這API也報錯

我有試過其他的寫法,發起請求,但都跳出一樣的訊息
CODE:

function uploadPostBlob(selectedFile) {

  //selectFile is File Object
  var metadata = {
    'name': selectedFile.name, // Filename at Google Drive
    'mimeType': selectedFile.mimeType, // mimeType at Google Drive
    'parents': ['root'], // Folder ID at Google Drive
  };
  var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
  var form = new FormData();
  form.append('metadata', new Blob([JSON.stringify(metadata)], {
    type: 'application/json'
  }));
  form.append('file', selectedFile);

  //https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable
  //https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart

  fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable', {
    method: 'POST',
    headers: new Headers({
      'Authorization': 'Bearer ' + accessToken,
      'Content-Type':'application/json; charset=UTF-8'

    }),
    body: form,
  }).then((res) => {

    return res.json();

  }).then(function(val) {
    console.log(val);
  });
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答