Yandex API Translator error - 401 API key is inval

2019-07-25 11:59发布

问题:

I registered with Yandex and got a Translate API Key. However when I try to translate with the following code:

<CFSET Key = "trnsl.1.1.2014091...........................">
<CFSET lang="en-de">
<CFSET text="Hallo World">


<CFHTTP URL="https://translate.yandex.net/api/v1.5/tr.json/translate?Key=#Key#&lang=#lang#&text=#text#"  METHOD = "GET">
</CFHTTP>


<CFOUTPUT>#CFHTTP.FileContent#</CFOUTPUT>

I get a 401 error "API key is invalid". I also tried with Javascript/CFML but got a similar result. I have checked the key, and it is current.

Anyone got something similar working?

回答1:

?Key=#Key#&lang=#lang#&text=#text#

This is going to sound a little crazy, but .. I think the reason is that the url parameter names are case sensitive. Since you are using ?Key= instead of ?key (all lower case) the receiving end thinks you did not supply an API key - at all. Hence the error. (Though "missing or invalid key" would be a little more accurate).

Try using ?key= (all lower case) instead and it should work.