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