Translator key does not work

Tran Tran 1 Reputation point
2022-02-09T11:02:00.71+00:00

Trying to use Microsoft Azure translator. Have got the necessary keys /see pic/. I work with PHP, so I have got that example from documentation:


$subscription_key = '........;
$endpoint = 'https://api.cognitive.microsofttranslator.com';
//$endpoint = 'https://api-eur.cognitive.microsofttranslator.com';
$path = "/detect?api-version=3.0";

$text = "Salve, mondo!";

if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}

function DictionaryLookup ($host, $path, $key, $params, $content) {

$headers = "Content-type: application/json\r\n" .  
    "Content-length: " . strlen($content) . "\r\n" .  
    "Ocp-Apim-Subscription-Key: $key\r\n" .  
    "X-ClientTraceId: " . com_create_guid() . "\r\n";  

$options = array (  
    'http' => array (  
        'header' => $headers,  
        'method' => 'POST',  
        'content' => $content  
    )  
);  
$context  = stream_context_create ($options);  
$result = file_get_contents ($host . $path . $params, false, $context);  
return $result;  

}

$requestBody = array (
array (
'Text' => $text,
),
);
$content = json_encode($requestBody);

$result = DictionaryLookup ($endpoint, $path, $subscription_key, "", $content);

$json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $json;
?>


I have got this result
Warning: file_get_contents(https://api.cognitive.microsofttranslator.com/detect?api-version=3.0): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in .............../private_html/telegram/detect.php on line 45
null


The keys are valid, but for some reason just do not work.
What could be the reason?172577-pic1.jpg

Azure AI Translator
Azure AI Translator
An Azure service to easily conduct machine translation with a simple REST API call.
484 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. GiftA-MSFT 11,176 Reputation points
    2022-02-10T04:50:15.643+00:00

    Hi, I wasn't able to reproduce this issue. Can you try a service like postman to verify that your key works? Furthermore, please refer to the API reference document. Based on the image provided, it seems you need to use the base url for Europe "api-eur.cognitive.microsofttranslator.com'. If you continue to experience issues, the best option would be to raise a support request for further investigation on account related issues. Thanks.

    0 comments No comments

  2. Tran Tran 1 Reputation point
    2022-02-10T06:49:28.61+00:00

    Hi, tx for trying to help.
    Don't know much about Postman.
    Can You, pls, check that key 52bfa6fdd1044d878f285ec8e08b1fe1
    If it works I'll regenerate it, but I am almost sure it is not.
    And if it is the case how can it be that Microsoft Azure provides the invalid keys?

    By the way, half a year ago this PHP script worked ok, but at that time it was another Microsoft account...

    And I did try this $endpoint = 'https://api-eur.cognitive.microsofttranslator.com'
    with no effect.
    Judging from the error code it seems to be the key problem.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.