400 Bad Request&获取Microsoft Translator API Azure令牌的更新

时间:2018-01-26 23:49:20

标签: azure datetime groovy libreoffice microsoft-translator

在我们被要求获得Azure订阅之前,我有一个旧系统一直在使用Microsoft Translator API。

正在翻译的一件事是证书上的日期,该证书是使用模板本身的groovy和jooscript中的函数从LibreOffice模板生成的。

日期正在从英文翻译成中文,并使用SimpleDateFormat(" dd MMMM yyyy")构建。

自更改/更新以来,我们收到了此错误:

引起:java.io.IOException:服务器返回HTTP响应代码:400为URL:http://api.microsofttranslator.com/v2/Http.svc/Translate?text=06+December+2017&from=en&to=zh-CHT

发送到API的请求:翻译?text = 06 + December + 2017& from = en& to = zh-CHT

发现当我们将文本更改为任何内容时,它会给出相同的响应,即翻译" Hello"会给出同样的错误。

预期回应:六日十二月二零一七年,即。 6天,12个月,2017年。虽然使用SimpleDateFormat将格式更改为dd MMM yyyy。

这是使用jooscript字段脚本在LibreOffice模板中调用的groovy函数:

public String translate(String text, String from, String to){
def client = new 
RESTClient("http://api.microsofttranslator.com/v2/Http.svc/Translate")
client.get(path:'', query:[text:"$text", from:"$from", to:"$to"], headers:
["Authorization":"Bearer ${getToken()}"]).xml
}

有谁知道为什么现在发生这个错误?

我想知道如何更新以下代码(如果需要更新)以使用新的Microsoft Translator API Azure商店,以便我可以包含Ocp-Apim-Subscription-Key,https://api.cognitive.microsoft.com/sts/v1.0/issueToken等等。我还需要身份证和秘密吗?

请注意,我没有编写原始代码,公司似乎无法访问提供翻译凭据的旧帐户等。

def id = *********
def secret = *********
def getToken = {
def client = new 
RESTClient("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13")

try{
    client.post(){
        type "application/x-www-form-urlencoded"
        urlenc client_id:id , client_secret:secret , scope: "http://api.microsofttranslator.com", grant_type: "client_credentials"
    }.json.access_token
}
catch(e){ e.printStackTrace(); return null;}

1 个答案:

答案 0 :(得分:1)

Microsoft DataMarket于2017年4月退休,因此客户端密码和客户端ID将不再有效。 Microsoft Translator API已移至Microsoft Azure门户。你需要:

  • 创建Azure帐户并
  • 订阅Azure,然后订阅
  • 订阅翻译API和
  • 对oAuth令牌进行次要代码更改。

您可以在https://cognitive.uservoice.com/knowledgebase/articles/1128340-microsoft-translator-moves-to-the-azure-portal

找到有关上述步骤的详细说明

如果您有任何其他问题,请告诉我们。

请, Gwenda 微软翻译团队