Coinbase API connectivity problem

9 次查看(过去 30 天)
Im trying to connecto to the Coinbase sandbox API. Ive done all the preliminary setup with the API keys and passphrase but I keep getting a 401 error. my code is below. Does anyone have an insights as to what I might be doing wrong here?
API_Secret = API_SECRET;
passphrase = PASSPHRASE;
API_KEY = API_KEY;
timestamp = num2str(round(posixtime(datetime())*1000));
message = [timestamp 'GET' '/accounts'];
signature = matlab.net.base64encode(HMAC(API_Secret, message,'SHA-256'));
url = 'https://api-public.sandbox.exchange.coinbase.com/accounts';
options = weboptions('ContentType', 'json', 'RequestMethod', 'get', ...
'HeaderFields', {'CB-ACCESS-KEY', API_KEY}, ...
'HeaderFields', {'CB-ACCESS-SIGN', signature}, ...
'HeaderFields', {'CB-ACCESS-TIMESTAMP', timestamp}, ...
'HeaderFields', {'CB-ACCESS-PASSPHRASE', passphrase});
response = webread(url, options);
data = jsondecode(response);

回答(1 个)

Venkat Siddarth
Venkat Siddarth 2023-3-9
编辑:Venkat Siddarth 2023-3-9
Hi @Xymbu,
I understand that you are encountering 401 error,when trying to connect with Coinbase API.
  • The 401 error typically indicates Unauthorized error.So,it is important to verify that the API URL and credentials you are using are correct and match the information provided by the API source.
  • If everything checks out,then I would recommend defining a single structure for HeaderFields option as follows
header={
'CB-ACCESS-KEY', API_KEY;
'CB-ACCESS-SIGN', signature;
'CB-ACCESS-TIMESTAMP', timestamp;
'CB-ACCESS-PASSPHRASE', passphrase}
%or
header=struct('CB-ACCESS-KEY', API_KEY, 'CB-ACCESS-SIGN', signature, 'CB-ACCESS-TIMESTAMP', timestamp, 'CB-ACCESS-PASSPHRASE', passphrase, 'Content-Type', 'application/json');
  • And, it is important noting that the syntax of ContentType option is different with different versions,so it is worth trying Content-Type inplace of ContentType.
I hope this resolves the issue,
Thanks and Regard,
Venkat Siddarth V.

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by