使用Javascript对SharePoint Online进行远程身份验证

使用Javascript对SharePoint Online进行远程身份验证,javascript,ajax,sharepoint,office365,sharepoint-online,Javascript,Ajax,Sharepoint,Office365,Sharepoint Online,我正在尝试通过从远程位置访问SharePoint online站点来检索列表项,到目前为止,我已经尝试使用SPSServices以及以下代码片段 function login(userID, password, url, successBlock, failBlock) { $.ajax({ url: 'https://login.microsoftonline.com/extSTS.srf', dataType: 'text

我正在尝试通过从远程位置访问SharePoint online站点来检索列表项,到目前为止,我已经尝试使用SPSServices以及以下代码片段

function login(userID, password, url, successBlock, failBlock) { 
        $.ajax({ 
            url: 'https://login.microsoftonline.com/extSTS.srf', 
            dataType: 'text', 
            type: 'POST', 
            crossDomain: true,
            data: getSAMLRequest(userID, password, url), 
            headers: { 
                Accept : "application/soap+xml; charset=utf-8" 
            }, 
            success: function(result, textStatus, jqXHR) { 
                var xmlDoc = $.parseXML(result); 
                var xml = $(xmlDoc) 
                var securityToken = xml.find("BinarySecurityToken").text(); 
                if (securityToken.length == 0) { 
                    failBlock(); 
                } 
                else { 
                    $.ajax({ 
                        url: url, 
                        dataType: 'text', 
                        type: 'POST', 
                        data: xml.find("BinarySecurityToken").text(), 
                        headers: { 
                            Accept : "application/x-www-form-urlencoded" 
                        }, 
                        success: function(result, textStatus, jqXHR) { 
                            successBlock(); 
                        }, 
                        error: function (jqXHR, textStatus, errorThrown) { 
                            failBlock(); 
                        } 
                    }); 
                } 
            }, 
            error: function (jqXHR, textStatus, errorThrown){ 
                failBlock(); 
            } 
        }); 
    } 

function getSAMLRequest(userID, password, url) { 
        return '<s:Envelope'
        + 'xmlns:s="http://www.w3.org/2003/05/soap-envelope"'
        + 'xmlns:a="http://www.w3.org/2005/08/addressing" ' 
        + 'xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> ' 
        + '<s:Header> ' 
            + '<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action> ' 
            + '<a:ReplyTo> ' 
            + '<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address> ' 
            + '</a:ReplyTo> ' 
            + '<a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To> ' 
            + '<o:Security ' 
                + 's:mustUnderstand="1" ' 
                + 'xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> ' 
                + '<o:UsernameToken> ' 
                    + '<o:Username>' + userID + '</o:Username> ' 
                    + '<o:Password>' + password + '</o:Password> ' 
                + '</o:UsernameToken> ' 
            + '</o:Security> ' 
        + '</s:Header> ' 
        + '<s:Body> ' 
            + '<t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"> ' 
                + '<wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> ' 
                    + '<a:EndpointReference> ' 
                        + '<a:Address>' + url + '</a:Address> ' 
                    + '</a:EndpointReference> ' 
                + '</wsp:AppliesTo> ' 
                + '<t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType> ' 
                + '<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType> ' 
                + '<t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>'
            + '</t:RequestSecurityToken>'
        + '</s:Body> ' 
        + '</s:Envelope>'; 
    } 
函数登录(用户ID、密码、url、成功块、失败块){
$.ajax({
网址:'https://login.microsoftonline.com/extSTS.srf', 
数据类型:“文本”,
键入:“POST”,
跨域:是的,
数据:getSAMLRequest(用户ID、密码、url),
标题:{
接受:“应用程序/soap+xml;字符集=utf-8”
}, 
成功:函数(结果、文本状态、jqXHR){
var xmlDoc=$.parseXML(结果);
var xml=$(xmlDoc)
var securityToken=xml.find(“BinarySecurityToken”).text();
如果(securityToken.length==0){
故障块();
} 
否则{
$.ajax({
url:url,
数据类型:“文本”,
键入:“POST”,
数据:xml.find(“BinarySecurityToken”).text(),
标题:{
接受:“application/x-www-form-urlencoded”
}, 
成功:函数(结果、文本状态、jqXHR){
successBlock();
}, 
错误:函数(jqXHR,textStatus,errorshown){
故障块();
} 
}); 
} 
}, 
错误:函数(jqXHR,textStatus,errorshown){
故障块();
} 
}); 
} 
函数getSAMLRequest(用户ID、密码、url){
返回“”
+ ' ' 
+“。可以通过将资源移动到同一域或启用CORS来修复此问题。”


如果有人对此提出更好的建议,我将不胜感激。

您确定microsoftonline.com服务器已经启用了CORS吗?如果是,您可能会觉得很有帮助,特别是jQuery中关于CORS的部分。您好,我无法确认office 365 server是否已经启用了CORS,是否还有其他方法可以做到这一点