【问题标题】:How to make REST call to MS Graph/OneDrive method with OAuth2如何使用 OAuth2 对 MS Graph/OneDrive 方法进行 REST 调用
【发布时间】:2017-06-19 21:50:31
【问题描述】:

我正在尝试使用 OneDrive API,并且我已通过他们的Application Registration Portal 成功注册了我的应用程序。我可以打电话成功拨打Javascript FilePicker SDK to upload and download files

这表明我已正确注册我的应用程序和正确的应用程序/客户端 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>

【问题讨论】:

    标签: rest microsoft-graph-api onedrive


    【解决方案1】:

    一种选择是使用Microsoft Graph JavaScript SDK,它可以帮助进行 REST 调用,包括通过 MS Graph 将文件上传到 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
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      相关资源
      最近更新 更多