2017-06-19 30 views
0

我正在嘗試使用OneDrive API,並且已通過他們的Application Registration Portal成功註冊了我的應用程序。我可以打電話Javascript FilePicker SDK to upload and download files如何使用OAuth2向MS Graph/OneDrive方法進行REST調用

這表明我有我的應用程序正確註冊和正確的應用程序/客戶端ID。

現在我想使用REST服務來上傳和下載文件,但我不知道如何發送身份驗證,我不知道如何撥打正確的URL。

我的第一個問題是:我如何使用在reg服務中創建的令牌進行REST調用?

我的第二個問題是:我應該用什麼語法上傳文件?我不知道在哪裏放置URL來撥打電話。

爲自己上傳的文件PUT發現here

<script type="text/javascript"> 
     function launchSaveToOneDrive(){ 
      var xhttp = new XMLHttpRequest(); 
      //Authorization: bearer {token} 
      xhttp.open("PUT", "/drive/items/{parent-id}:/{filename}:/content", false); 
      xmlhttp.setRequestHeader("Authorization", "Bearer-xxxxxxxxxxxxxxxxxxx"); 
      xhttp.setRequestHeader("Content-type", "text/plain"); 
      xhttp.send(); 
      var response = JSON.parse(xhttp.responseText); 
     } 
     </script> 

回答

1

一種選擇是使用Microsoft Graph JavaScript SDK可與REST求助電話可以上傳文件通過MS圖形到OneDrive。該庫適用於客戶端JavaScript和Node for JavaScript服務器應用程序。

檢查示例下的Browser folder以瞭解如何在客戶端應用程序中使用SDK。上傳文件看起來像這樣(請參閱完整代碼的鏈接):

// file variable is from the contents of an input field for example 
    var file = document.querySelector('input[type=file]').files[0]; 

    // after user selects a file from the file picker 

    client 
     .api('/me/drive/root/children/Book.xlsx/content') 
     .put(file, (error, response) => { 
      // supports callbacks and promises 
     }); 
相關問題