問題描述
我是谷歌 API 的新手.我能夠從應用程序腳本上傳此文件,并且通過該腳本上傳的任何文件都僅存儲到我的驅動器中.但是如何使用javascript做到這一點.谷歌示例:https://developers.google.com/drive/web/quickstart/quickstart-js顯示如何執行此操作,但文件會上傳到授權該應用程序的同一用戶的驅動器.如何將其限制在我的驅動器上.謝謝
I am new to google api. I am able to do this file upload from app script and any file which is uploaded through that script get stored to my drive only. But how to do this using javascript. Example on google : https://developers.google.com/drive/web/quickstart/quickstart-js shows how to do this but file gets uploaded to the same user's drive who is authorizing the app. How to restrict it to my drive only. Thanks
推薦答案
簡單的答案是你不能使用 JavaScript.原因是 JavaScript 與 OAuth2 一起使用,這需要您請求用戶許可才能訪問您的數據.
Simple answer is you cant with JavaScript. The reason being is that JavaScript works with OAuth2 this requires that you ask the user permission to access your data.
如果您想讓它訪問您的驅動器帳戶,您必須將刷新令牌保存在某個地方,然后在加載腳本時發送它.JavaScript 是客戶端的,因此任何檢查頁面上的代碼的人都將獲得他們需要的所有信息,以使用您的驅動器帳戶做任何他們想做的事情.出于安全考慮,這是個壞主意.
If you want to have it access your drive account you would have to save the refreshtoken some place and then send that when ever the script was loaded. JavaScript is client sided so anyone that checked the code on the page would then have all the information they needed to do what ever they wanted with your drive account. Security wise that's a bad idea.
我建議您考慮使用像 PHP 這樣的服務器端腳本語言.您可能需要考慮一個服務帳戶.注意:一切都將歸服務帳戶所有,因此您必須授予服務帳戶訪問您的 Google Drive 文件的權限,或者您需要將驅動器文件移動到服務帳戶.
I recommend you look into using a server sided scripting language like PHP. You might want to consider a service account. Note: everything will be owned by the service account so you will either have to give the Service account access to your Google Drive files or you will need to move your drive files to the Service account.
如果您不希望服務帳戶擁有您可以使用普通 Oauth2 保存的文件,請保存刷新令牌,然后將其存儲在服務器端代碼中,那里不會有太多的安全風險.
If you don't want the service account to have the files you could go with normal Oauth2 save the refresh token and then store it in the server sided code there wont be as much of security risk there.
這篇關于使用 javascript 將文件從任何人上傳到我的谷歌驅動器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!