2017-01-02 45 views
0

我試圖創建Azure EasyAPI以從我的應用程序中的身份提供商(Microsoft)獲取某些用戶信息。然而,我在網上找到的所有例子都出現錯誤,而且我在stackoverflow上找到的答案都沒有幫助。從身份提供商和Azure EasyAPI獲取用戶信息

錯誤:

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)); 
    }); 

     //... 

}}; 

感謝您的幫助。

+0

也許這個答案將幫助您:http://stackoverflow.com/questions/35878102/get-facebook-auth-token-from-azure-easy-api –

+0

這改變了錯誤 - 現在,在我的C#代碼,當我等待API調用時,它需要很長時間並最終超時。在我的Azure輸出中,我得到「愚蠢:GetIdentity Request:hostname = vflash.azurewebsites.net,port = 443,path = /。auth/me?provider = [object Object],method = GET,+一個非常長的字符串 – user3007447

回答

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(); 

    } 
} 
+0

感謝您的幫助,但是我仍然遇到問題,如果可以,請查看我對原始問題的更新。對不起,我並不真正瞭解node.js.謝謝! – user3007447

+0

您能否獲得'request.azureMobile.user.token'中存儲的'authenticationToken'? –

+0

不管我改變代碼,我總是收到同樣的錯誤,除了getIdentity函數和console.log外,我仍然收到輸入錯誤的意外結束,並且我的括號和括號全部匹配正確 – user3007447

相關問題