简体   繁体   中英

Microsoft Translator Text API doesn't work

I want to use Microsoft Translator API but I am unable to use it.

I created a Microsoft Azure account as specified in the documentation ( http://docs.microsofttranslator.com/text-translate.html ) and I created a resource.

When I call the web service to get an access toke, every time I get an exception because a time out..

This is my code (it's Apex, similar to 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.

Do you know why I get this?

Thanks

You should replace [myAPIKey] with the right key. You can get it through https://www.microsoft.com/cognitive-services

在此处输入图片说明

EDIT The answer above is related to GET operations. For POST, you should include the 'Ocp-Apim-Subscription-Key' header:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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