在单个页面加载中使用rest api获取访问令牌 [英] Get access token using rest api in a single loading of page

查看:72
本文介绍了在单个页面加载中使用rest api获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我创建了一个webpart,我需要访问令牌来获取我已经设法获取令牌的更多数据,但它正在加载页面三次然后它显示数据。这种多次重新加载页面正在减慢速度。


请建议我在单页加载中获取和使用令牌的方式,以便减少页面加载时间并提高页面性能。  


谢谢,

Aakash




Aakash Rajput

解决方案


这是预期的行为,当使用SharePoint Online Rest API时,我们需要先获取Access Token最后,FedAuthCookie获得表格摘要值,并在Rest API标题中使用,如下面的代码演示:

< script type =" text / ECMAScript的" SRC =" HTTP://code.jquery.com/jquery-1.8.3.min.js">< /脚本> 

< script language =" ecmascript"类型= QUOT;文本/ ECMAScript的">

var tokenReq ='<?xml version =" 1.0"编码= QUOT; UTF-8英寸;?>";
tokenReq + ='< soap:Envelope xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance" ;;的xmlns:XSD = QUOT; HTTP://www.w3.org/2001/XMLSchema" ;;的xmlns:SOAP = QUOT; HTTP://schemas.xmlsoap.org/soap/envelope/">";
tokenReq + ='< soap:Body>';
tokenReq + ='< GetUpdatedFormDigestInformation xmlns =" http://schemas.microsoft.com/sharepoint/soap/" ;; />";
tokenReq + ='< / soap:Body>';
tokenReq + ='< / soap:Envelope>';

//你应该根据你的实际要求设置这些值
var usr ='xxx@xxx.onmicrosoft.com';
var pwd ='xxx';
var siteFullUrl =" https://xxx.sharepoint.com/sites/dev" ;;

var loginUrl = siteFullUrl +" / _ forms / default.aspx?wa = wsignin1.0" ;;
var authReq ='< 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">'
authReq + ='< ; s:Header>'
authReq + ='< a:Action s:mustUnderstand =" 1"> http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue< ; / a:操作>'
authReq + ='< a:回复>'
authReq + ='< a:地址> http://www.w3.org/2005/08/寻址/匿名< / a:地址>'
authReq + ='< / a:ReplyTo>'
authReq + ='< a:至s:mustUnderstand =" 1"> https ://login.microsoftonline.com/extSTS.srf< / a:To>'
authReq + ='< o:Security s:mustUnderstand =" 1" xmlns:o =" http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">'
authReq + ='< ; o:UsernameToken>'
authReq + ='< o:用户名>'+ usr +'< / o:用户名>'
authReq + ='< o:密码>'+ pwd +'< / o:密码>'
authReq + ='< / o:UsernameToken>'
authReq + ='< / o:安全>'
authReq + =' < / s:Header>'
authReq + ='< s:Body>'
authReq + ='< t:RequestSecurityToken xmlns:t =" http://schemas.xmlsoap。 org / ws / 2005/02 / trust">< wsp:AppliesTo xmlns:wsp =" http://schemas.xmlsoap.org/ws/2004/09/policy">'
authReq + ='< a:EndpointReference>'
authReq + ='< a:地址>'+ loginUrl +'< / a:地址>'
authReq + ='< / a:EndpointReference>'
authReq + ='< / wsp:AppliesTo>'
authReq + ='< t:KeyType> http://schemas.xmlsoap。 org / ws / 2005/05 / identity / NoProofKey< / t:KeyType>'
authReq + ='< t:RequestType> http://schemas.xmlsoap.org/ws/2005/02/trust/问题< / t:RequestType>'
authReq + ='< t:TokenType> urn:oasis:names:tc:SAML:1.0:assertion< / t:TokenType>'
authReq + =' < / t:RequestSecurityToken>'
authReq + ='< / s:Body>'
authReq + ='< / s:Envelope>';

函数startScript(){
getToken();
}

var token;
//第1步:我们从STS
函数获取令牌getToken()
{


.support.cors = true; //启用跨域查询


.ajax({
类型:'POST',
数据:authReq,
crossDomain:true,//没有效果,请参阅上面的支持。
contentType:'application / soap + xml; charset = utf-8',
url:'https://login.microsoftonline.com/extSTS.srf' ,
dataType:'xml',
success:function(data,textStatus,result){
//从响应数据中提取令牌
// var token =

Hi,

I have created a webpart where I need the access token to fetch further data I have managed to get the token but it is loading the page thrice and then it displays the data. This multiple reloading of page is slowing the things up.

Please suggest some way by which I can get and take use of token in a single page load so that the page loading time can be decreased and improve the performance of page. 

Thanks,
Aakash


Aakash Rajput

解决方案

Hi,

This is expected behavior, when using SharePoint Online Rest API, we need to firstly get Access Token, then FedAuthCookie, finally, get form digest value and used in Rest API headers like the code demo below:

 <script type="text/ecmascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>

    <script language="ecmascript" type="text/ecmascript">

        var tokenReq = '<?xml version="1.0" encoding="utf-8"?>';
        tokenReq += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
        tokenReq += '  <soap:Body>';
        tokenReq += '    <GetUpdatedFormDigestInformation xmlns="http://schemas.microsoft.com/sharepoint/soap/"; />';
        tokenReq += '  </soap:Body>';
        tokenReq += '</soap:Envelope>';

        // you should set these values according your actual request
        var usr = 'xxx@xxx.onmicrosoft.com';
        var pwd = 'xxx';
        var siteFullUrl = "https://xxx.sharepoint.com/sites/dev";

        var loginUrl = siteFullUrl + "/_forms/default.aspx?wa=wsignin1.0";
        var authReq =   '<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">'
        authReq +=      '  <s:Header>'
        authReq +=      '    <a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>'
        authReq +=      '    <a:ReplyTo>'
        authReq +=      '      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>'
        authReq +=      '    </a:ReplyTo>'
        authReq +=      '    <a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To>'
        authReq +=      '    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">'
        authReq +=      '      <o:UsernameToken>'
        authReq +=      '        <o:Username>' + usr + '</o:Username>'
        authReq +=      '        <o:Password>' + pwd + '</o:Password>'
        authReq +=      '      </o:UsernameToken>'
        authReq +=      '    </o:Security>'
        authReq +=      '  </s:Header>'
        authReq +=      '  <s:Body>'
        authReq +=      '    <t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"><wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">'
        authReq +=      '      <a:EndpointReference>'
        authReq +=      '        <a:Address>' + loginUrl + '</a:Address>'
        authReq +=      '      </a:EndpointReference>'
        authReq +=      '      </wsp:AppliesTo>'
        authReq +=      '      <t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>'
        authReq +=      '      <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>'
        authReq +=      '      <t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>'
        authReq +=      '    </t:RequestSecurityToken>'
        authReq +=      '  </s:Body>'
        authReq +=      '</s:Envelope>';

        function startScript() {
          getToken();
        }

        var token;
        // Step 1: we get the token from the STS
        function getToken()
        {


.support.cors = true; // enable cross-domain query


.ajax({ type: 'POST', data: authReq, crossDomain: true, // had no effect, see support.cors above contentType: 'application/soap+xml; charset=utf-8', url: 'https://login.microsoftonline.com/extSTS.srf', dataType: 'xml', success: function (data, textStatus, result) { // extract the token from the response data // var token =


这篇关于在单个页面加载中使用rest api获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆