我想要:在Bing Translator服务的帮助下翻译文本

问题:我收到消息:


  Web令牌的末尾必须具有签名。传入令牌确实
  令牌末尾没有签名。


编码前的令牌:

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier=Subtitles&http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider=https://datamarket.accesscontrol.windows.net/&Audience=http://api.microsofttranslator.com&ExpiresOn=1411488480&Issuer=https://datamarket.accesscontrol.windows.net/&HMACSHA256=DsdsshfhhegefrgradfsdffqI=


码:

var request = require('request');
var querystring = require('querystring');
/**
 * Demo for translating a 'Hello world' from en to uk with help of Bing Translator service
 */
(function() {
    var makeTranslateRequest = function(token) { //I provided you my token at this point
        var opts = {
            text: 'Hello world',
            from: 'en-gb',
            to: 'ru'
        };

        request.get({
            url: "http://api.microsofttranslator.com/v2/Http.svc/Translate?contentType=text/plain&from=" + opts.from + "&to=" + opts.to + "&text=" + opts.text,
            headers: {
                'Authorization': encodeURIComponent('Bearer' + token).replace(/'/g,"%27").replace(/"/g,"%22")
            }
        },function(err,res,body) {
            var translated = body;
            console.log('translated: ' + translated);
        });
    }

    /**
     * Get token and make translate request in a callback
     */
    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){
        var decoded = decodeURIComponent(body.replace(/\+/g,  " "))
        var token = JSON.parse(decoded).access_token;
        makeTranslateRequest(token);
    });
})();

最佳答案

我阅读了有关here令牌的XML解码的内容。因此,我决定用XMLHTTPRequest替换节点请求模块。

工作示例:

var request = require('request');
var querystring = require('querystring');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

/**
 * Demo for translating a 'Hello world' from en to uk with help of Bing Translator service
 */
(function() {
    //INSTEAD OF request.get({}) I use XMLHTTPRequest! Also I stoped to decoding/encoding token. That's the solution.
    var makeTranslateRequest = function(token) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "http://api.microsofttranslator.com/v2/Http.svc/Translate?from=en&to=ru&text=Good", true);
        xmlhttp.setRequestHeader('Authorization', 'Bearer ' + token);
        xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                console.log(xmlhttp.responseText);
            }
        }
        xmlhttp.send();
    }

    /**
     * Get token and make translate request in a callback
     */
    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) {
        var token = JSON.parse(body).access_token;
        makeTranslateRequest(token);
    });
})();

07-28 02:50