javascript - 从 node.js 使用 SharePoint 2013 进行身份验证

标签 javascript node.js rest authentication sharepoint

我有一个用 JavaScript 编写的命令行脚本,它需要连接到远程 SharePoint 站点上的 REST Api,但我不知道如何进行身份验证。我可以使用表单例份验证在浏览器中登录,并且通过检查请求我应该能够在 node.js 中重现它以获得适当的身份验证 cookie 或 token 。但是,我实际上得到了 403 Forbidden。我不知道这是否是最好的方法,但我找不到太多关于它的信息。这是我的脚本:

var http = require('http');
var querystring = require('querystring');

var postData = querystring.stringify({
  'ctl00$PlaceHolderMain$signInControl$UserName': 'restapi',
  'ctl00$PlaceHolderMain$signInControl$password': 'my_password',
  'ctl00$PlaceHolderMain$signInControl$login': 'Sign In'
});

var options = {
  hostname: 'sharepoint.example.com',
  port: 80,

  path: '/_layouts/15/Authenticate.aspx?Source=%2F',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ', res.headers);
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(postData);
req.end();

响应头是:

{ 'cache-control': 'private',
  'content-type': 'text/plain; charset=utf-8',
  server: 'Microsoft-IIS/8.5',
  'x-sharepointhealthscore': '0',
  'x-aspnet-version': '4.0.30319',
  sprequestguid: '366d149d-79af-b07c-1764-dec7001b46a2',
  'request-id': '366d149d-79af-b07c-1764-dec7001b46a2',
  'x-frame-options': 'SAMEORIGIN',
  sprequestduration: '7',
  spiislatency: '0',
  'x-forms_based_auth_required': 'http://sharepoint.example.com/_login/default.aspx?ReturnUrl=/_layouts/15/error.aspx&Source=%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F',
  'x-forms_based_auth_return_url': 'http://sharepoint.example.com/_layouts/15/error.aspx',
  'x-msdavext_error': '917656; Access denied. Before opening files in this location, you must first browse to the web site and select the option to login automatically.',
  'x-powered-by': 'ASP.NET',
  microsoftsharepointteamservices: '15.0.0.4569',
  'x-content-type-options': 'nosniff',
  'x-ms-invokeapp': '1; RequireReadOnly',
  date: 'Sat, 27 Jun 2015 10:53:28 GMT',
  connection: 'close',
  'content-length': '13' }

有什么建议吗?

最佳答案

您确定 REST API 允许 CORS 吗?因为 403 Forbidden 可能意味着不允许您从外部联系该端点。

该远程 SharePoint 应用程序是否使用 AD 进行身份验证? 如果是这样,您可能需要检查这个包 https://www.npmjs.com/package/activedirectory

关于javascript - 从 node.js 使用 SharePoint 2013 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088005/

相关文章:

javascript - 为什么jplayer在iphone上玩?

javascript - Meteor.js 应用程序无法启动 | "You are attempting to run Meteor as the ' root' super 用户...”

javascript - Chrome WebSocket 连接立即关闭

java - Spring RESTTemplate getForObject 方法不支持返回内容类型 'null'

security - 构建 API - 发送密码的安全方式

php - 为在另一个网页中加载的网页添加后退按钮

javascript - javascript 中的原型(prototype)继承不起作用

javascript - Node.js 全局变量属性被清除

node.js - 我必须包括 :433 to access HTTPS on AWS Elastic Beanstalk

java - 自定义响应头 Jersey/Java