Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  149] [ 3]  / answers: 1 / hits: 25347  / 10 Years ago, wed, september 24, 2014, 12:00:00

I have:
I used Node.js request module to get authorization token:



Working code without promise



var request = require('request');
var querystring = require('querystring');

var requestOpts = querystring.stringify({
client_id: 'Subtitles',
client_secret: 'X............................s=',
scope: 'http://api.microsofttranslator.com',
grant_type: 'client_credentials'
});

request.post({
encoding: 'utf8',
url: https://datamarket.accesscontrol.windows.net/v2/OAuth2-13,
body: requestOpts
}, function(err, res, body) { //CALLBACK FUNCTION
var token = JSON.parse(body).access_token;
amkeAsyncCall(token);
});


I want:
It takes some time to get that token. In turn I need makeAsyncCall from getToken callback. So I decide to use a request-promise from here.



Problem: request-promise seems don't work for me at all.



The same (not working) code with promise:



    var rp = require('request-promise');
var querystring = require('querystring');

var requestOpts = {
encoding: 'utf8',
uri: 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
method: 'POST',
body: querystring.stringify({
client_id: 'Subtitles',
client_secret: 'Xv2Oae6Vki4CnYcSF1SxSSBtO1x4rX47zhLUE/OqVds=',
scope: 'http://api.microsofttranslator.com',
grant_type: 'client_credentials'
})
};

rp(requestOpts)
.then(function() {
console.log(console.dir);
})
.catch(function() {
console.log(console.dir);
});

More From » node.js

 Answers
8

I use the node.js package unirest.



var unirest = require('unirest');
var dataObj = {};
var Request = unirest.post('http://127.0.0.1:' + port + '/test/4711DE/de');
Request.headers({ 'Accept': 'application/json' })
.type('json')
.send(JSON.stringify(dataObj))
.auth({
user: 'USERNAME',
pass: 'PASSWORD',
sendImmediately: true
})
.end(function (response) {
assert.equal(200, response.statusCode);
// ...
});

[#69355] Sunday, September 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arron

Total Points: 663
Total Questions: 119
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;