如何使用OAuth2对MS

如何使用OAuth2对MS

本文介绍了如何使用OAuth2对MS Graph/OneDrive方法进行REST调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OneDrive API,并且已经通过其应用程序注册成功注册了我的应用程序门户网站.我可以成功调用 JavaScript FilePicker SDK来上传和下载文件

I am trying to use the OneDrive API and I have successfully registered my app through their Application Registration Portal. I can call successfully call the Javascript FilePicker SDK to upload and download files

这表明我已经正确注册了我的应用程序,并且具有正确的应用程序/客户端ID.

That demonstrates that I have my app registered properly and the proper app/client-id's.

现在,我想使用REST服务上载和下载文件,但是我不确定如何发送身份验证,也不知道如何调用正确的URL.

Now I'd like to use the REST services to upload and download files but I'm not sure how to send authentication and I don't know how to make the call to the proper URL.

我的第一个问题是:如何使用在reg服务中创建的令牌进行REST调用?

My first question is: How can I use the token I created in the reg service to make a REST call?

我的第二个问题是:上传文件应使用哪种语法?我不知道将URL放在哪里进行呼叫.

My second question is: What syntax should I use to upload a file? I don't know where to put the URL to make the call.

此处

    <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>

推荐答案

一种选择是使用 Microsoft Graph JavaScript SDK ,可以帮助进行REST调用,包括通过MS Graph将文件上传到OneDrive.该库可与客户端JavaScript和JavaScript服务器应用程序的Node一起使用.

One option is to use the Microsoft Graph JavaScript SDK that can help with REST calls including uploading files to OneDrive through the MS Graph. The library works with client side JavaScript and Node for JavaScript server apps.

检查浏览器文件夹在示例下,了解如何在客户端应用程序中使用SDK.上传文件看起来像这样(有关完整代码,请参见该链接):

Check the Browser folder under samples to see how to use the SDK in a client app. Uploading a file would look something like this (see that link for the full code):

    // 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
        });

这篇关于如何使用OAuth2对MS Graph/OneDrive方法进行REST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:56