简体   繁体   English

Microsoft Translator Text API不起作用

[英]Microsoft Translator Text API doesn't work

I want to use Microsoft Translator API but I am unable to use it. 我想使用Microsoft Translator API,但无法使用它。

I created a Microsoft Azure account as specified in the documentation ( http://docs.microsofttranslator.com/text-translate.html ) and I created a resource. 我按照文档( http://docs.microsofttranslator.com/text-translate.html )中的说明创建了一个Microsoft Azure帐户,并创建了一个资源。

When I call the web service to get an access toke, every time I get an exception because a time out.. 当我调用Web服务获取访问权限时,每次都会因超时而出现异常。

This is my code (it's Apex, similar to Java) : 这是我的代码(它是Apex,类似于Java):

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Length', '3495');
req.setEndpoint('https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=[myAPIKey]');
req.setTimeout(20000);
HttpResponse res = h.send(req);

If I remove my API key or I the content length from the header, I get an error from Microsoft. 如果我删除API密钥或标题中的内容长度,则会收到Microsoft错误消息。

Do you know why I get this? 你知道我为什么得到这个吗?

Thanks 谢谢

You should replace [myAPIKey] with the right key. 您应该用正确的密钥替换[myAPIKey]。 You can get it through https://www.microsoft.com/cognitive-services 您可以通过https://www.microsoft.com/cognitive-services获得它

在此处输入图片说明

EDIT The answer above is related to GET operations. 编辑上面的答案与GET操作有关。 For POST, you should include the 'Ocp-Apim-Subscription-Key' header: 对于POST,您应包括“ Ocp-Apim-Subscription-Key”标头:

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Length', '3495');
req.setHeader('Ocp-Apim-Subscription-Key', '[INSERT_HERE_YOUR_TOKEN]');
req.setEndpoint('https://api.cognitive.microsoft.com/sts/v1.0/issueToken');
req.setTimeout(20000);
HttpResponse res = h.send(req);

It works fin now. 现在可以正常工作了。

I edit my code and it is ok : 我编辑我的代码,没关系:

HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(theURL);
req.setHeader('Content-Type','application/xml');
Http binding = new Http();
HttpResponse res = binding.send(req);

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM