2017-03-06 14 views
1

Microsoft Translator API用の認証トークンを取得したいとします。 、「メッセージ」401: {「からstatusCode」:それは私に次のエラーを与える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); 


?> 

:これは私のコードです。サブスクリプションキーが欠落が原因で拒否された「アクセスに要求を行うときにサブスクリプションキーが含まれていることを確認しますAPI。 " }

これは、以下のウェブサイトではキーが無効である可能性がありますが、そのキーが同じウェブサイトで有効であることを保証しています。

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

私はオンラインAuthenticationtokenを取得する方法についていくつかの例を見つけたが、彼らは時代遅れです。

AuthenticationTokenを取得するにはどうすればいいですか?Microsoftが自分のキーを認識するためにはどうすればよいですか?

答えて

0

は、サブスクリプション・キー間違って渡している - ?サブスクリプションキーは、ヘッダ(OCP-APIM-スクリプションキー)またはURLでクエリ文字列パラメータとして渡さなければならない を購読キー=

そして、 Azure認知サービスダッシュボードで生成されたKey1またはKey2を使用する必要があります。 -

FYI 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にも入力してください。

関連する問題