Microsoft 文本翻译 API 不起作用
Microsoft Translator Text API doesn't work
我想使用 Microsoft Translator API 但我无法使用它。
我按照文档 (http://docs.microsofttranslator.com/text-translate.html) 中的说明创建了一个 Microsoft Azure 帐户,并创建了一个资源。
当我调用网络服务获取访问令牌时,每次我都会因为超时而出现异常..
这是我的代码(它是 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);
如果我删除我的 API 密钥或我从 header 中删除内容长度,我会收到来自 Microsoft 的错误消息。
你知道我为什么会收到这个吗?
谢谢
您应该将 [myAPIKey] 替换为正确的密钥。你可以通过https://www.microsoft.com/cognitive-services
获取
编辑
上面的答案与 GET 操作有关。对于 POST,您应该包括 '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);
现在可以正常使用了。
我编辑了我的代码,没问题:
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);
谢谢
我想使用 Microsoft Translator API 但我无法使用它。
我按照文档 (http://docs.microsofttranslator.com/text-translate.html) 中的说明创建了一个 Microsoft Azure 帐户,并创建了一个资源。
当我调用网络服务获取访问令牌时,每次我都会因为超时而出现异常..
这是我的代码(它是 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);
如果我删除我的 API 密钥或我从 header 中删除内容长度,我会收到来自 Microsoft 的错误消息。
你知道我为什么会收到这个吗?
谢谢
您应该将 [myAPIKey] 替换为正确的密钥。你可以通过https://www.microsoft.com/cognitive-services
获取编辑 上面的答案与 GET 操作有关。对于 POST,您应该包括 '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);
现在可以正常使用了。
我编辑了我的代码,没问题:
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);
谢谢