如何修复 Coinbase api 无效签名错误?

How to fix Coinbase api invalid signature error?

我想通过 coinbase api 端点了解我的所有帐户,因此请遵循此 link:https://developers.coinbase.com/api/v2?shell#list-accounts 我已经这样做了:

        $path = '/v2/accounts';
        $signature = $this->signature($path);

        try {
            $client = new \GuzzleHttp\Client();

            $response = $client->request('GET', 'https://api.coinbase.com' . $path, [
                'headers' => [
                    'Content-Type' => 'application/json',
                    'CB-ACCESS-KEY' => $this->key,
                    'CB-ACCESS-SIGN' => $signature,
                    'CB-ACCESS-TIMESTAMP' => time(),
                    'CB-VERSION' => '2015-04-08',
                ]
            ]);

            echo $response->getBody();
        } catch (\Throwable $th) {
            dd($th);
        }

签名函数为:

    public function signature($request_path = '', $body = '', $timestamp = false, $method = 'GET')
    {
        $body = is_array($body) ? json_encode($body) : $body;
        $timestamp = $timestamp ? $timestamp : time();

        $what = $timestamp . $method . $request_path . $body;

        return base64_encode(hash_hmac("sha256", $what, base64_decode($this->secret)), true);
    }

当我写 $path = '/v2/time' 时它可以工作但是 $path = '/v2/accounts/' 它给了我一个错误:

{"errors":[{"id":"authentication_error","message":"invalid signature"}]

我把我的签名功能改成了这样:

    public function signature($cbPro, $request_path = '', $body = '', $method = 'GET', $timestamp = false)
    {
        $body = is_array($body) ? json_encode($body) : $body;
        $timestamp = $timestamp ? $timestamp : time();

        $what = $timestamp . $method . $request_path . $body;

        if ($cbPro)
            return base64_encode(hash_hmac("sha256", $what, base64_decode($this->secret), true)); // For CB Pro/Exchange
        else
            return hash_hmac('SHA256', $what, $this->secret); // The solution for CB api v2
    }

Coinbase 和 Coinbase Pro 似乎没有相同的签名。现在我正在使用 Coinbase api,我使用函数的第二个 return,第一个用于 CB Pro,或 Exchange。一个简单的 hash_hmac() 解决了我的问题。没看够