我正在尝试将Microsoft Azure转换器示例调用从curl转换为RCurl。
示例如下:
curl -X POST https://api.cognitive.microsofttranslator.com/translate\?api-version=3.0\&from=en\&to=nb -H "Ocp-Apim-Subscription-Key: ****" -H "Content-Type: application/json" -d "[{'Text':'Hello, what is your name?'}]"
所以我认为这可能有效:
r <- POST(url="https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=nb",
add_headers(
.headers=
c("Ocp-Apim-Subscription-Key"="****")),
encode="json",
content_type("application/json"),
body=list(text = "Hello, what is your name?"))
但是
stop_for_status(r)
告诉我
Error: Unauthorized (HTTP 401).
和
content(r, "parsed", "application/json")
说
$error
$error$code
[1] 401000
$error$message
[1] "The request is not authorized because credentials are missing or invalid."
从外壳运行,用我的实际凭据替换为****,我获得了不错的挪威语翻译。显然,似乎没有提供标题。
任何提示和建议都表示赞赏。
谢谢。