iT邦幫忙

0

請問google drive API的問題?

QQ 2019-08-01 10:47:373184 瀏覽

Q1.
我在已授權的 JavaScript 來源設定
http://127.0.0.1:8848http://localhost:8848
https://ithelp.ithome.com.tw/upload/images/20190801/201132814I9eXOD1Q0.jpg

我在自己的 http://127.0.0.1:8848 上跑網站
登入時卻跳出
https://ithelp.ithome.com.tw/upload/images/20190801/20113281rY5eaB1QkM.png

但是我用http://localhost:8848 則能成功登入
127.0.0.1和localhost不是一樣的嗎?

Q2.
GOOGLE DRIVE API是否有辦法斷點上傳?
我自己有試過如果上傳一個300MB的東西
上傳到100MB時我關掉WIFE 然後馬上連回去
就是可能一秒內我再連線回去 他是可以繼續上傳的

但如果我關掉網路 沒有馬上連回去
比如可能過了十幾秒 上傳就會失敗 只能重傳

Q3.
我post多檔案時會出現這個錯誤訊息?403錯誤
https://ithelp.ithome.com.tw/upload/images/20190801/20113281CUAlx8Ekxj.png
這是指我一瞬間post太多檔案嗎 有些檔案會沒上傳成功
我把input tag讀到的的檔案用for迴圈一個個Post

我目前想到的方式一個是用setTimeout不要讓他一口氣全部上傳
一個則是如果status==403 就再上傳 傳到成功為止
還是有其他更好的方法??

document.getElementById("upload_button").onclick = function() {
	var selectedFile = document.getElementById('input').files;
	
	// for (let i = 0; i < selectedFile.length; i++) {
	// 	setTimeout(function timer() {
	// 		uploadPostBlob(selectedFile[i]);
	// 		console.log(i);
	// 	}, i * 150)
	// }
	
	for (let i = 0; i < selectedFile.length; i++) {
		uploadPostBlob(selectedFile[i]);
	}
	
}
function uploadPostBlob(selectedFile) {
	
	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=multipart', {
		method: 'POST',
		headers: new Headers({
			'Authorization': 'Bearer ' + accessToken
		}),
		body: form,
	}).then((res) => {
		
		// if(res.status==403){
		// 	re_upload(form,accessToken)
		// }else{
		// 	return res.json();
		// }
		
		return res.json();
		
	}).then(function(val) {
		
		console.log(val);
	});
}

function re_upload(form,accessToken){
	fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', {
		method: 'POST',
		headers: new Headers({
			'Authorization': 'Bearer ' + accessToken
	
		}),
		body: form,
	}).then((res) => {	
		
		if(res.status==403){
			re_upload(form,accessToken)
		}else{
			return res.json();
		}
		
	}).then(function(val) {
		
		console.log(val);
	});
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
dragonH
iT邦超人 5 級 ‧ 2019-08-01 10:59:49

1 .

但是我用http://localhost:8848 則能成功登入
127.0.0.1和localhost不是一樣的嗎?

沒去研究

不過你貼的錯誤訊息

感覺不是這個的關係

這個感覺跟你的情況一樣

2 .

GOOGLE DRIVE API是否有辦法斷點上傳?

api doc

的 Upload types 有提到

Resumable upload: uploadType=resumable. For more reliable transfer, especially important with large files. 

或許你可以試試

3 .

handler errors

403: User rate limit exceeded
The per-user limit has been reached. This may be the limit from the Developer Console or a limit from the Drive backend.
Suggested actions:

Raise the per-user quota in the Developer Console project.
If one user is making a lot of requests on behalf of many users of a G Suite domain, consider a Service Account with authority delegation (setting the quotaUser parameter).
Use exponential backoff to retry the request.
0

GOOGLE 的 APi,都是需要有別名域名,不能直接用ip的。
所以你用ip指向連結的方式,來跑api會出現錯誤的。

建議你本機測試時,可以利用一下host文件處理假設域名來跑。

我要發表回答

立即登入回答