【问题标题】:Coinbase Api Key Authentication Invalid TokenCoinbase Api Key Authentication Invalid Token
【发布时间】:2021-06-17 18:47:34
【问题描述】:

我可能遗漏了一些非常明显的东西,但我无法弄清楚我的请求有什么问题。

有没有人设法连接到 Coinbase API 并指出我的错误。

请求:

    <?php 

// Keys from Coinbase
$key = 'public_key';
$secret = 'secret_key';

date_default_timezone_set("UTC");

// CB-ACCESS-TIMESTAMP
$cb_access_timestamp = time();

// CB-ACCESS-KEY
$cb_access_key = $key;

// CB-ACCESS-SIGN
$method = 'GET';
$request_path = 'v2/user';
$body = '';
$pre_hash = $cb_access_timestamp . $method . $request_path . $body;

$cb_access_sign = hash_hmac('sha256', $pre_hash, $secret);


// Start request
$ch = curl_init("https://api.coinbase.com/v2/user");


curl_setopt($ch, CURLOPT_HEADER, array(
    "CB-ACCESS-KEY:". $cb_access_key,
    "CB-ACCESS-SIGN:". $cb_access_sign,
    "CB-ACCESS-TIMESTAMP:". $cb_access_timestamp
    )
);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

$response = curl_exec($ch);

echo 'response:<pre>' . print_r($response, true). '</pre>';

curl_close($ch);

回复:

{"errors":[{"id":"invalid_token","message":"The access token is invalid"}]}

【问题讨论】:

  • “requestPath 是 URL 的完整路径和查询参数,例如:/v2/exchange-rates?currency=USD。” (developers.coinbase.com/docs/wallet/api-key-authentication) – 您的路径似乎缺少 @ 987654325@一开始。
  • 感谢您的帮助,这是导致此功能无法正常工作的少数问题之一

标签: php curl api-key coinbase-api coinbase-php


【解决方案1】:

所以看到这让我非常悲伤,我想我会分享我是如何设法让它工作的

以下是调整后的代码:

<?php
// Keys from Coinbase
$key = 'public key';
$secret = 'private key';

date_default_timezone_set("UTC");

// CB-ACCESS-TIMESTAMP
$cb_access_timestamp = time();

// CB-ACCESS-KEY
$cb_access_key = $key;

// CB-ACCESS-SIGN
$method = 'GET';
$request_path = '/v2/user'; // CHANGE 1 - my request path had the incorrect slashes being used
$body = '';
$pre_hash = $cb_access_timestamp . $method . $request_path . $body;

$cb_access_sign = hash_hmac('sha256', $pre_hash, $secret);

// Start request
$ch = curl_init("https://api.coinbase.com/v2/user");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$headers = array();
$headers[] = "CB-ACCESS-KEY:  $cb_access_key";
$headers[] = "CB-ACCESS-SIGN:  $cb_access_sign";
$headers[] = "CB-ACCESS-TIMESTAMP:  $cb_access_timestamp";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // CHANGE 2 - Need to use CURLOPT_HTTPHEADER. I was previously using CURLOPT_HEADER which was incorrect.

//return the transfer as a string

$response = curl_exec($ch);

curl_close($ch);

?>

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2018-10-01
    • 2018-04-20
    相关资源
    最近更新 更多