【发布时间】:2019-01-11 05:56:37
【问题描述】:
我正在尝试使用 combell api 来自动化我的托管。我需要为 API 请求生成 HMAC 身份验证标头。我正在使用 Guzzle。
我的当前代码与我的标题生成此错误:
客户端错误:GET https://api.combell.com/v2/accounts 导致
401 Unauthorized响应:{“error_code”:“authorization_hmac_invalid”,“error_text”:“hmac 无效。” }
我的控制器
class GuzzleController extends Controller
{
protected $api_key;
protected $api_secret;
public function __construct()
{
$this->api_key = env('API_KEY');
$this->api_secret = env('API_SECRET');
}
protected function hmacHandler() {
$key = $this->api_key;
$req_method = 'get';
$path_query = 'https://api.combell.com/';
$timestamp = time();
$nonce = substr(md5(uniqid(mt_rand(), true)), 0, 8);
$content = '';
$valueToSign = $this->api_key
. $req_method
. urlencode($path_query)
. $timestamp
. $nonce
. $content;
$signedValue = hash_hmac('sha256', $valueToSign, $this->api_secret, true);
$signature = base64_encode($signedValue);
return sprintf('hmac %s:%s:%s:%s', $this->api_key, $signature, $nonce, $timestamp);
}
public function index() {
dd($this->getTestData());
}
public function getTestData() {
$client = new Client();
$uri = 'https://api.combell.com/v2/accounts';
$header = ['headers' => ['Authorization' => $this->hmacHandler()]];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}
}
我不确定我的 hmac 函数是否不正确,或者我是否以错误的方式使用 Guzzle 授权标头,有什么提示吗?
【问题讨论】: