【问题标题】:Getting User information from identity provider and Azure EasyAPI从身份提供者和 Azure EasyAPI 获取用户信息
【发布时间】:2017-01-04 19:18:52
【问题描述】:

我正在尝试创建一个 Azure EasyAPI 以从我的应用程序中的身份提供者 (microsoft) 获取一些用户信息。但是,我从网上找到的所有示例中都遇到了错误,而且我在 stackoverflow 上找到的答案都没有帮助。

错误:

Azure Log:    
    : Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at IncomingMessage.<anonymous> (D:\home\site\wwwroot\node_modules\azure-mobile-apps\src\auth\getIdentity.js:35:55)
    at emitNone (events.js:72:20)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:903:12)
    at doNTCallback2 (node.js:439:9)
    at process._tickCallback (node.js:353:17)

代码:

    module.exports = {
"get": function (request, response, next) {
    request.azureMobile.user.getIdentity('microsoftaccount').then(function (data) {
        var accessToken = data.microsoftaccount.access_token;
        var url = 'https://apis.live.net/v5.0/me/?method=GET&access_token=' + accessToken;
        var requestCallback = function (err, resp, body) {
            if (err || resp.statusCode !== 200) {
                console.error('Error sending data to the provider: ', err);
                response.send(statusCodes.INTERNAL_SERVER_ERROR, body);
            } else {
                try {
                    var userData = JSON.parse(body);
                    response.send(200, userData);
                } catch (ex) {
                    console.error('Error parsing response from the provider API: ', ex);
                    response.send(statusCodes.INTERNAL_SERVER_ERROR, ex);
                }
            }
            var req = require('request');
        var reqOptions = {
            uri: url,
            headers: { Accept: "application/json" }
        };
        req(reqOptions, requestCallback);
        };  
        }).catch(function (error) {
        response.status(500).send(JSON.stringify(error));
    });

        //...

}};

感谢您的帮助。

【问题讨论】:

  • 也许这个答案会对你有所帮助:stackoverflow.com/questions/35878102/…
  • 这改变了错误——现在,在我的 c# 代码中,当我等待 API 调用时,它需要很长时间并且最终超时。在我的 Azure 输出中,我得到“愚蠢:GetIdentity Request: hostname=vflash.azurewebsites.net, port=443, path=/.auth/me?provider=[object Object], method=GET, + a very long string

标签: node.js azure azure-mobile-services


【解决方案1】:

在用于 Node.js 的 Azure 移动应用程序的 API Documentation 中,它表示 getIdentity 方法接受身份验证提供程序的名称作为参数并返回一个承诺,该承诺会在成功时产生身份信息。

所以你的代码应该是这样的:

module.exports = {
    "get": function (req, res, next) {
        req.azureMobile.user.getIdentity('microsoftaccount').then(function (data) {
            var accessToken = data.microsoftaccount.access_token;
            //...
        }).catch(function (error) {
            res.status(500).send(JSON.stringify(error));
        });
    }
}

获取用户信息的另一个选项是调用/.auth/me 端点。

var https = require('https');

module.exports = {
    "get": function (request, response, next) {

        var token = request.azureMobile.user.token;

        var options = {
            hostname: '<yourappname>.azurewebsites.net',
            port: 443,
            path: '/.auth/me',
            method: 'GET',
            headers: {
                'x-zumo-auth': token
            }
        };

        var req = https.request(options, (res) => {

            var str = '';
            res.on('data', (d) => {
                str += d;
            });

            res.on('end', function () {
                console.log(str);
                response.status(200).type('application/json').json(str);
            });
        });

        req.on('error', (e) => {
            console.error(e);
        });
        req.end();

    }
}

【讨论】:

  • 感谢您的帮助。但是,我仍然遇到问题。如果可以的话,请看看我对原始问题的更新。抱歉,我真的不知道 node.js。谢谢!
  • 你能得到存储在request.azureMobile.user.token中的authenticationToken吗?
  • 好吧,无论我将代码更改为什么,我都会收到同样的错误。我取出了除 getIdentity 函数和 console.log 之外的所有内容,但仍然出现意外的输入错误结束。而且我的括号和括号都匹配正确。
  • 嗨@user3007447,我更新了我的回复。您可以尝试拨打/.auth/me 获取用户信息吗?
  • 不幸的是,新代码在 Azure 日志中没有提供任何输出,并且在我的客户端应用程序中没有提供任何数据。
猜你喜欢
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多