温馨提示:本文翻译自stackoverflow.com,查看原文请点击:jquery - Accessing Microsoft Graph API for excel data on using AJAX request
ajax api excel jquery microsoft-graph

jquery - 使用AJAX请求访问Microsoft Graph API以获取Excel数据

发布于 2020-04-08 09:56:29
function requestToken() {
        $.ajax({
            "async": true,
            "crossDomain": true,
            "url": endpointUrl, // Pass your tenant name instead of sharepointtechie
            "method": "POST",
            "headers": {
                "content-type": "application/x-www-form-urlencoded"
            },
            "data": {
                "grant_type": "password",
                "client_id ": clientId, //Provide your app id
                "client_secret": clientSecret,
                "scope ": "https://graph.microsoft.com/.default",
                "userName": "xxxxxxxxx",
                "password": "xxxxxxxx",
                "redirect_uri" : "xxxxxxx"
            },
            success: function (response) {
                console.log(response);
                token = response.access_token;



                  $.ajax({
                      url: 'xxxxxx',
                      type: 'GET',
                      dataType: 'json',
                      beforeSend: function (xhr) {
                          xhr.setRequestHeader('Authorization', 'Bearer ' + token + '');
                      },
                      data: {},
                      success: function (results) {
                          console.log(results);
                          debugger;
                      },
                      error: function (error) {
                          console.log("Error in getting data: " + error);
                      }
                  });
            }

        })
    }

我的意思是向MS Graph Api发出请求,以便从OneDrive上托管的Excel电子表格中获取单元格和行数据。

一切都很好,但是我想知道解决这个问题的最佳实践是什么。

我可以使用client-id和client-secret编写一个AJAX请求来认证使用,但是这会将这些凭证公开给客户端浏览器。

我如何仍可以向API发出请求,但要确保应用凭据安全。

干杯,乔希

查看更多

提问者
Joshua Pauline
被浏览
66
Jason Johnston 2020-01-31 21:36

来自浏览器的客户端请求不会使用客户端机密,而是会使用隐式授予流您可以在该链接上阅读有关该流程如何工作的所有详细信息。

MSAL.js使此操作易于实现,并且它们的存储库中有许多示例。