【问题标题】:How to make a valid hmac authentication header如何制作有效的 hmac 身份验证标头
【发布时间】: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 授权标头,有什么提示吗?

Combell API documentation

【问题讨论】:

    标签: php laravel rest guzzle


    【解决方案1】:

    根据响应,Authorization 标头很好(添加标头的语法看起来不错),但值不正确。

    文档声明路径必须是相对的。尝试替换

    $path_query = 'https://api.combell.com/';

    $path_query = '/v2/accounts';(或/accounts,文档不清楚)。

    内容不应该是必需的,因为正文是空的。

    【讨论】:

      【解决方案2】:

      我相信您的标题缺少信息。

      signingString 标识编码中使用的部分,也必须包含在标头中。

      例子:

      $date = gmdate("D, d M Y H:i:s") . " GMT";
      $signingString = "Date: $date";
      $signature = base64_encode(hash_hmac('sha1', $signingString, $secret, true));
      $authorization = "hmac username=\"$user\", algorithm=\"hmac-sha1\", headers=\"Date\", signature=\"$signature\"";
      
      $headers = ['Content-Type' => 'application/json',
              'Date' => $date,
              'Authorization' => $authorization,
              'Content-MD5' => $bodyHash ];
      
      $options = [
         'headers' => $header,
         'json' => $body];
      $response = $client->request('POST', $uri, $options);
      

      在本例中,'Date' 字段在标头中传递一个值,用于编码(signingString)与密码和'Authorization' 标头标识在 'headers' 子字段中抓取哪些字段作为解码的一部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-28
        • 2019-09-20
        • 1970-01-01
        • 2015-09-09
        • 1970-01-01
        • 2019-06-19
        • 2021-09-26
        • 2015-03-07
        相关资源
        最近更新 更多