Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

signature - Api key authentication for coinbase

I'm trying to write a request for API coinbase.com, but I can not correctly generate a signature. I've been trying to find my mistake for 2 days, but I can not. I analyzed the code for other languages on the page: https://developers.coinbase.com/docs/wallet/api-key-autumnicathion but I do not see any differences in implementation.

Help me please.

<?php
$g_coinbase_key = 'KcxisxqmWRVgtwsj';
$g_coinbase_secret = 'isOLGBLaEkCy3ROQMvmjonGmXK0KRmUS';

$time = time();
$method = "GET";
$path = '/v2/accounts/';
$sign = base64_encode(hash_hmac("sha256", $time.$method.$path, $g_coinbase_secret));
$ch = curl_init('https://api.coinbase.com'.$path);
$headers = array(
    "CB-VERSION: 2017-10-26",
    "CB-ACCESS-SIGN: ".$sign,
    "CB-ACCESS-TIMESTAMP: ".$time,
    "CB-ACCESS-KEY: ".$g_coinbase_key,
    "Content-Type: application/json"
);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);
?>

Result:

{"errors":[{"id":"authentication_error","message":"invalid signature"}]}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create signature like this:

$time = time();
$method = "GET";
$path = 'accounts';
$sign = base64_encode(hash_hmac("sha256", $time.$method.$path, base64_decode($g_coinbase_secret), true));

and replace

$ch = curl_init('https://api.coinbase.com'.$path);

with

$ch = curl_init('https://api.coinbase.com/v2/');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...