如何获取SharePoint online(O365)的token

尽管微软给我们提供了认证的API,不过在实际开发中,我们还是经常需要用到认证所需的Token的。本篇文章就介绍如何获取SharePoint Online(O365)的认证Token。

这里我们可以用Java,C#,C/C++等一切语言来实现。JavaScript除外,因为涉及到跨域的问题。

一、获取Security Token。

通过post方式访问 [https://login.microsoftonline.com/extSTS.srf],其中请求正文为如下内容。

<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>[username]</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>[endpoint]</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>
在PostMan中的演示截图:


相应报文中会返回一个 security token,用于获取Access Token,如下图所示。


二、获取Access Token

通过post方式访问 [https://yourdomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0],请求正文为刚刚获取到的Security Token。如下图所示:


对应的相应信息如下:


我们可以看到相应头中设置了两个cookie,rtFa和FedAuth,这两个cookie在后续的访问中都需要加在请求中。

三、获取Request Digest(最终我们想要的Token)

通过Post访问 [https://yourdomain.sharepoint.com/_api/contextinfo],把上一步获取的两个cookie信息加上。如下图所示。


得到的响应信息如下所示。


这就是我们所要的最终的Token啦!在后续对SharePoint的访问中,只需要加上这个token以及之前的两个cookie就可以了,如下图所示。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Node.js获取SharePoint的PendingRequest需要使用到SharePoint REST API。PendingRequest是一个异步请求,需要先发送请求并等待返回,才能获取到相关的PendingRequest。 首先,在Node.js中需要使用请求模块,向SharePoint发起一次Get请求,通过访问 `https://<siteurl>/_api/contextinfo`来获取到访问许可Cookie,从而获得并发许可。 接下来使用POST方法,向 `https://<siteurl>/_api/contextinfo` 发送请求,获取Pending请求token: ``` var https = require('https'); var cookie = ''; var server = 'https://<siteurl>'; function getToken(cb) { var options = { hostname: server.replace('https://', ''), path: '/_api/contextinfo', method: 'POST', headers: { 'Authorization': 'Bearer ' + cookie, 'Content-Type': 'application/json;', 'Accept': 'application/json;' } }; var req = https.request(options, function(res) { var data = ''; res.on('data', function(chunk) { data += chunk; }); res.on('end', function() { try { var token = JSON.parse(data).d.GetContextWebInformation.FormDigestValue; cb(null, token); } catch (e) { cb('Error parsing response: ' + e.message); } }); }); req.on('error', function(e) { cb('Error while retrieving token:' + e.message); }); req.end(); } ``` 从response数据中解析出 `GetContextWebInformation.FormDigestValue`,这个token将被用于PendingRequest的下一步。 下面通过POST方法来实现投入到PendingRequest的一些代码中: ``` function addPendingRequest(cb) { getToken(function(err, token) { if (err) return cb(err); var options = { hostname: server.replace('https://', ''), path: '/_api/contextinfo', method: 'POST', headers: { 'Accept': 'application/json;odata=verbose', 'ContentType': 'application/json;odata=verbose', 'Authorization': 'Bearer ' + cookie, 'X-RequestDigest': token, } }; var req = https.request(options, function(res) { var data = ''; res.on('data', function(chunk) { data += chunk; }); res.on('end', function() { try { var result = JSON.parse(data); cb(null, result.d); } catch (e) { cb('Error parsing response: ' + e.message); } }); }); req.on('error', function(e) { cb('Error while retrieving token:' + e.message); }); req.end(); }); } ``` 这段代码中的 `addPendingRequest` 将Post请求发送给 `contextinfo`,然后从返回的JSON数据结果中解析出响应的PendingRequest对象。 最后,在Node.js中,调用 `addPendingRequest` 函数并传入回调函数,等待PendingRequest的结果即可: ``` addPendingRequest(function(err, pendingRequest) { if (err) return console.error(err); console.log(pendingRequest); // Returns the entire pending request object }); ``` 这就是在 Node.js 使用 SharePoint REST API获取 PendingRequest的方法。需要注意的是在获取PendingRequest对象时需要先获取Context Web信息,从而获取投入PendingRequest的Token

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值