使用azure-graphapi模块为节点js更新用户

时间:2016-03-30 11:06:20

标签: node.js azure restify azure-ad-graph-api

我正在尝试使用Node.js技术更新Azure帐户上的用户。我正在使用azure-graphapi模块发送请求并进行初始化。 以下是我的代码。

var GraphAPI = require('azure-graphapi');
var graph = new GraphAPI(appSettings.oauthOptions.tenantId, appSettings.oauthOptions.clientId, appSettings.oauthOptions.clientSecret);
var reqHeaders = { "content-type": "application/json" };
var reqBody = {
            "department": "Sales",
            "usageLocation": "US"
        }
    var person = {
        userId: userID
    };

graph.patch('users/f0eceb4f-xxxx-409a-xxxx-4e3exx4e3157', JSON.stringify(reqBody), reqHeaders, function (err, user) {
        if (!err) {
            console.log(user);
        }
        else {
            console.log(err);
        }
    });

即使在提供content-Type标头之后,它也会给我一个错误,因为“{[错误:图形API错误:400(错误请求)Content-Type标头值丢失。] statusCode:400}”

如果有人可以帮助我解决这个问题,那将是非常有帮助的。

1 个答案:

答案 0 :(得分:2)

您正在使用的此模块中存在多个错误。为了使代码有效,我们应该在GraphAPI.js中的源代码node_modules/auzre-graphapi中进行一些其他修改:

Line 195开始,有一个if条件stmt,作者似乎忘记定义自line 199以来使用的content,并且只有在必须将帖子正文解析为buffer对象,它将设置内容类型标题。因此,我们可以快速修改代码:

if (data) {
        if (Buffer.isBuffer(data)) {
            options.headers['Content-Type'] = contentType;
        } else if (!contentType) {
            content = data;
            if (typeof content === 'string') {
                options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
                options.headers['Content-Length'] = content.length;
            } else if (content !== null && typeof content === 'object') {
                content = JSON.stringify(content);
                options.headers['Content-Type'] = 'application/json';
                options.headers['Content-Length'] = content.length;
            }
        } else {
            if (typeof contentType === 'string') {
                options.headers['Content-Type'] = contentType;
            } else if (contentType['Content-type'] !== null) {
                options.headers['Content-Type'] = contentType['Content-type'];
            }
        }
    }

然后将标题设置为您的代码:var reqHeaders = { "Content-type": "application/json" };

BTW,正如update user document所指的那样,如果成功,它将在没有响应主体的情况下响应204,因此您的代码将打印" undefined"如果成功。

2016年4月19日更新

由于作者不再维护包,他已经为通用Graph API创建了一个新包graph-service。请参阅https://github.com/fhellwig/azure-graphapi/issues/5#issuecomment-211392546

相关问题