我想获得 Microsoft Translator API 的身份验证令牌。这是我的代码:
<?php
//1. initialize cURL
$ch = curl_init();
//2. set options
//Set to POST request
curl_setopt($ch, CURLOPT_POST,1);
// URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken');
//return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//whether to include header in the output. here set to false
curl_setopt($ch, CURLOPT_HEADER, 0);
//pass my subscription key
curl_setopt($ch, CURLOPT_POSTFIELDS,array(Subscription-Key => '<my-key>'));
//CURLOPT_SSL_VERIFYPEER- Set to false to stop verifying certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//3. Execute the request and fetch the response. check for errors
$output = curl_exec($ch);
if ($output === FALSE) {
echo "cURL Error" . curl_error($ch);
}
//4. close and free up the curl handle
curl_close($ch);
//5. display raw output
print_r($output);
?>
它给了我以下错误:{“statusCode”:401,“message”:“由于缺少订阅密钥而拒绝访问。请确保在向 API 发出请求时包含订阅密钥。” }
根据下面的网站,这可能意味着密钥无效,但我确保密钥在同一网站上有效。
http://docs.microsofttranslator.com/oauth-token.html
我确实在网上找到了一些关于如何获取 Authenticationtoken 的示例,但它们已经过时了。
我怎样才能获得微软识别我的密钥的 AuthenticationToken/achieve?