首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >机器翻译报错AuthFailure.SignatureFailure?

机器翻译报错AuthFailure.SignatureFailure?

提问于 2019-03-07 22:43:36
回答 2关注 2查看 1.7K

下面的代码自己写的,报错AuthFailure.SignatureFailure,The provided credentials could not be validated. Please check your signature is correct.麻烦帮看看哪里的问题,谢谢

<?php
header('Content-type:text/html; Charset=utf-8');
$SecretId = 'xxx';
$SecretKey = 'xxx';
$uriData = 'tmt.tencentcloudapi.com';

$SourceText = "hello world!";
$Source = "en";
$Target = "zh";
$region = 'ap-chengdu';

$test = new fanyi($SecretId, $SecretKey, $uriData);
$ret = $test->translate($SourceText, $Source, $Target, $region);

$ret = json_decode($ret, true);
print_r($ret);

class fanyi {
	protected $SecretId;
	protected $SecretKey;
	public function __construct($SecretId, $SecretKey, $uriData) {
		$this->SecretId = $SecretId;
		$this->SecretKey = $SecretKey;
		$this->uriData = $uriData;
	}
	public function translate($SourceText, $Source, $Target, $region) {
		$arr = array(
			'Action' => 'TextTranslate',
			'ProjectId' => 0,
			'Region' => $region,
			'SecretId' => $this->SecretId,
			'SourceText' => urlencode($SourceText),
			'Source' => $Source,
			'Target' => $Target,
			'Nonce' => mt_rand(10000, 99999),
			'Timestamp' => time(),
			'Version' => '2018-03-21'
		);
        $arr['Signature'] = urlencode(self::sign($this->uriData, $arr, $this->SecretKey));
		print_r($arr);
		return self::curlPost("https://$this->uriData", $arr);
	}
	public static function sign($uriData, $params, $SecretKey){
		ksort($params, SORT_STRING);
		$params = http_build_query($params);
		$srcStr = "$uriData/?$params";
		$signStr = base64_encode(hash_hmac('sha1', $srcStr, $SecretKey, true));
		return $signStr;
	}
	public static function curlPost($url = '', $postData = '', $options = array()) {
		if (is_array($postData)) {
			$postData = http_build_query($postData, true);
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		if (!empty($options)) {
			curl_setopt_array($ch, $options);
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
}

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档