1

我正在使用数字网络。我正在使用炮弹示例。我在本地计算机上运行以下代码。

这是我的客户端代码

<script>document.getElementById('digits-sdk').onload = function() {
  Digits.init({ consumerKey: 'my consumer key' });
Digits.embed({
  container: '#my-digits-container',
  theme: {
   /* Input fields borders */
  },
  phoneNumber: '+91',

})
.done(onLogin) /*handle the response*/
    .fail(onLoginFailure);
  };


  function onLoginFailure(loginResponse) {
    console.log('Digits login failed.');
    //setDigitsButton('Verify again');
  }
  /* Validate and log use in. */

function onLogin(loginResponse){
  // Send headers to your server and validate user by calling Digits’ API
  //var oAuthHeaders = loginResponse.oauth_echo_headers;
  var oAuthHeaders = parseOAuthHeaders(loginResponse.oauth_echo_headers);
$.ajax({
      type: 'POST',
      url: '/digits',
      data: oAuthHeaders,
      success: onDigitsSuccess
    });
  //  setDigitsButton('Step 2.....');

}
function parseOAuthHeaders(oAuthEchoHeaders) {
    var credentials = oAuthEchoHeaders['X-Verify-Credentials-Authorization'];
    var apiUrl = oAuthEchoHeaders['X-Auth-Service-Provider'];
console.log(apiUrl);
    return {
      apiUrl: apiUrl,
      credentials: credentials
    };
  }

  function onDigitsSuccess(response) {
    console.log(response.phoneNumber);
    setDigitsNumber(response.phoneNumber);
  }
   function setDigitsNumber(phoneNumber) {
    document.getElementById('notr').value = phoneNumber;
    console.log('Digits phone number retrieved.');
  }
</script>

在上面的代码中,我只更改了消费者密钥。所以忽略这一点。

这是我的服务器代码

var express = require("express");
var app = express();
var router = express.Router();
var path = __dirname + '/static/';
app.use(express.static(__dirname + '/static'));
router.use(function (req,res,next) {
  console.log("/" + req.method);
  next();
});
router.get("/",function(req,res){

  res.sendFile(path + "homepage9.html");

});

router.get("/about",function(req,res){
  res.sendFile(path + "about.html");
});

router.get("/contact",function(req,res){
  res.sendFile(path + "contact.html");
});

app.use("/",router);

app.use("*",function(req,res){
  res.sendFile(path + "404.html");
});

app.listen(3000,function(){
  console.log("Live at Port 3000");
});


var fs = require('fs');
var nconf = require('nconf');
var url = require('url');
var request = require('request');
router.post('/digits', function (req, res) {
console.log("digits entered") 
 var apiUrl = req.body['apiUrl']
  var credentials = req.body['credentials']
  var verified = true;
  var messages = [];
  if (credentials.indexOf('oauth_consumer_key="' + 'my consumer key' + '"') == -1) {
    verified = false;
    messages.push('The Digits API key does not match.');
  }
  var hostname = url.parse(req.body.apiUrl).hostname;
  if (hostname != 'api.digits.com' && hostname != 'api.twitter.com') {
    verified = false;
    messages.push('Invalid API hostname.');
  }

  // Do not perform the request if the API key or hostname are not verified.
  if (!verified) {
    return res.send({
      phoneNumber: "",
      userID: "",
      error: messages.join(' ')
    });
  }

  // Prepare the request to the Digits API.
  var options = {
    url: apiUrl,
    headers: {
      'Authorization': credentials
    }
  };

  // Perform the request to the Digits API.
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      // Send the verified phone number and Digits user ID.
      var digits = JSON.parse(body)
      return res.send({
        phoneNumber: digits.phone_number,
        userID: digits.id_str,
        error: ''
      });
    } else {
      // Send the error.
      return res.send({
        phoneNumber: '',
        userID: '',
        error: error.message
      });
    }
  });
});

但是在节点控制台上我得到

无法读取未定义的属性“apiUrl”。

在谷歌浏览器控制台上我得到

加载资源失败:服务器响应状态为 500(内部服务器错误)

任何人都可以帮助我做错什么。

同样在炮弹示例中,我发现它在任何地方都没有使用消费者密钥。这是为什么?

4

0 回答 0