如何从node.js进行SharePoint 2013的休息调用?

时间:2016-03-07 15:44:25

标签: node.js rest http sharepoint sharepoint-2013

在我的node.js应用程序中,我想对sharepoint 2013进行休息调用。这是node.js代码

function downloadData(res, key, success, fail) {    
    var opts = url.parse(key);


    opts.method = 'GET';
    opts.headers = {"Accept":"application/json;odata=verbose","X-RequestDigest":"0x146E2BDC9A21FDA62E793A0FC9793A530A5C6688BE194FB051703C8529B01A16607931C77D26EE61FA360AD04526F476A052FAC9B3C9277A5463AC20A27C3543,07 Mar 2016 15:37:18 -0000"};
    opts.headers['Content-Type'] = 'application/json;odata=nometadata';

    var req = http.request(opts, function(response) {
        var str = '';

        //another chunk of data has been recieved, so append it to `str`
        response.on('data', function (chunk) {
            str += chunk;
        });

        //the whole response has been recieved, so we just print it out here
        response.on('end', function () {
            success(res, str, key);
        });
    })

    //req.write(data);
    req.end();
}

我从客户端获取了摘要值,我做了一个警告声明,并将其复制。我在一个示例网址上尝试了它,如果我粘贴在浏览器上,它会起作用。当我运行时,我得到401 unauthorized

有谁知道错误是什么?

由于

1 个答案:

答案 0 :(得分:0)

首先,X-RequestDigest Header用于验证客户端请求,但用于身份验证。

假设您的SharePoint网站使用NTLM authentication(默认),您可以利用实现HTTP NTLM身份验证流程的httpntlm package并允许执行HTTP请求,如下所示:

var credentials = {
    username: 'jdoe',
    password: 'password',
    domain: 'contoso'
};
var webUrl = "http://contoso.intranet.com";
var fileUrl = "/Shared Documents/guide.docx";


httpntlm.get({
    url: webUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/$value",
    username: credentials.username,
    password: credentials.password,
    domain: credentials.domain
}, function (err, res) {
    if (err) console.log(err);


    console.log(res.body); //print data
});