2017-03-06 170 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也可以。