2017-03-06 174 views
1

我想获取Microsoft Translator API的身份验证令牌。这是我的代码:如何获取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,“消息”:“由于缺少订阅密钥拒绝访问确保发出请求时,包括订阅密钥一个API“。 }

这可能意味着密钥根据下面的网站无效,但我确保密钥在同一网站上有效。

http://docs.microsofttranslator.com/oauth-token.html

我做了网上关于如何让Authenticationtoken找到一些例子,但它们已经过时。

我怎样才能获得AuthenticationToken /实现微软认识我的钥匙?

回答

0

你传递订阅的关键错误 - ? 的订购金钥应在头部(OCP-APIM-认购键)或在URL中的查询参数传递订阅密钥=

而且您应该使用由Azure认知服务仪表板生成的Key1或Key2。

仅供参考 - M $已经提供用于测试的令牌生成,这应该给你一个线索被使用的按键为此目的: http://docs.microsofttranslator.com/oauth-token.html

这里是它转换为字符串从EN到工作的PHP脚本FR(它是基于一个叫做由BoLiQuan WP-弹头,翻译过时的WP插件,我已经修改了这一目的):

<?php 
 

 
define("CLIENTID",'<client-name>'); // client name/id 
 
define("CLIENTSECRET",'<client-key>'); // Put key1 or key 2 here 
 
define("SOURCE","en"); 
 
define("TARGET","fr"); 
 

 

 
class WstHttpRequest 
 
{ 
 
\t function curlRequest($url, $header = array(), $postData = ''){ 
 
\t \t $ch = curl_init(); 
 
\t \t curl_setopt($ch, CURLOPT_URL, $url); 
 
\t \t if(!empty($header)){ 
 
\t \t \t curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
 
\t \t } 
 
\t \t curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
 
\t \t curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 
\t \t if(!empty($postData)){ 
 
\t \t \t curl_setopt($ch, CURLOPT_POST, TRUE); 
 
\t \t \t curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postData) ? http_build_query($postData) : $postData); 
 
\t \t } 
 
\t \t $curlResponse = curl_exec($ch); 
 
\t \t curl_close($ch); 
 
\t \t return $curlResponse; 
 
\t } 
 
} 
 

 
class WstMicrosoftTranslator extends WstHttpRequest 
 
{ 
 
\t private $_clientID = CLIENTID; 
 
\t private $_clientSecret = CLIENTSECRET; 
 
\t private $_fromLanguage = SOURCE; 
 
\t private $_toLanguage = TARGET; 
 

 
\t private $_grantType = "client_credentials"; 
 
\t private $_scopeUrl = "http://api.microsofttranslator.com"; 
 
\t private $_authUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; 
 
\t 
 
\t // added subscription-key 
 
\t private function _getTokens(){ 
 
\t \t try{ 
 
\t \t \t $header = array('Ocp-Apim-Subscription-Key: '.$this->_clientSecret); 
 
\t \t \t $postData = array(
 
\t \t \t \t 'grant_type' => $this->_grantType, 
 
\t \t \t \t 'scope' => $this->_scopeUrl, 
 
\t \t \t \t 'client_id' => $this->_clientID, 
 
\t \t \t \t 'client_secret' => $this->_clientSecret 
 
\t \t \t); 
 
\t \t \t $response = $this->curlRequest($this->_authUrl, $header, $postData); 
 
\t \t \t if (!empty($response)) 
 
\t \t \t \t return $response; \t \t 
 
\t \t } 
 
\t \t catch(Exception $e){ 
 
\t \t \t echo "Exception-" . $e->getMessage(); 
 
\t \t } 
 
\t } 
 

 
\t function translate($inputStr){ 
 
\t \t $params = "text=" . rawurlencode($inputStr) . "&from=" . $this->_fromLanguage . "&to=" . $this->_toLanguage; 
 
\t \t $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params"; 
 
\t \t $accessToken = $this->_getTokens(); 
 
\t \t $authHeader = "Authorization: Bearer " . $accessToken; 
 
\t \t $header = array($authHeader, "Content-Type: text/xml"); 
 
\t \t $curlResponse = $this->curlRequest($translateUrl, $header); 
 
\t \t 
 
\t \t $xmlObj = simplexml_load_string($curlResponse); 
 
\t \t $translatedStr = ''; 
 
\t \t foreach((array)$xmlObj[0] as $val){ 
 
\t \t \t $translatedStr = $val; 
 
\t \t } 
 
\t \t return $translatedStr; 
 
\t } 
 

 
} 
 

 
function bing_translator($string) { 
 
\t $wst_microsoft= new WstMicrosoftTranslator(); 
 
\t return $wst_microsoft->translate($string); 
 
} 
 

 
echo bing_translator("How about translating this?"); 
 
?>

0

将您的密钥也添加到URL中。

curl_setopt($ch, CURLOPT_URL, 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key={your key}'); 

但是,在CURLOPT_POSTFIELDS也可以。