Bing使用JavaScript和Node.js翻译API

时间:2014-09-23 15:57:07

标签: javascript node.js api bing

我想:借助Bing翻译服务翻译文本

问题:我收到消息:

  

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

1 个答案:

答案 0 :(得分:2)

我读了一些关于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);
    });
})();