【问题标题】:Updating user using azure-graphapi module for Node js使用 Node js 的 azure-graphapi 模块更新用户
【发布时间】:2016-04-19 09:23:49
【问题描述】:

我正在尝试使用 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 标头后,它也会向我抛出一个错误“{ [Error: Graph API Error: 400 (Bad Request) Content-Type header value missing.] statusCode: 400 }”

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

【问题讨论】:

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


    【解决方案1】:

    您使用的这个模块有几个错误。为了使代码能够正常工作,我们应该对源代码GraphAPI.js 中的node_modules/auzre-graphapi 做一些额外的修改:

    Line 195开头,有一个if条件stmt,作者似乎忘记定义content,它自line 199开始使用,并且仅当您必须将帖子正文解析为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" };

    顺便说一句,正如update user document 所指,如果成功,它将响应 204 而没有响应正文,因此如果成功,您的代码将打印“未定义”。

    2016 年 4 月 19 日更新

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 2019-10-18
      • 2017-09-24
      相关资源
      最近更新 更多