Coinbase API not able to connect due to 401 error code
27 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am currently trying to connect to coinbases API, but am new to the field. I am not entirely sure where I went wrong, I keep getting error status 401 (invalid api key). The secret, passcode, and key are all currently set to " * ".
%% Set up api keys/passwords
%secret = '***************************************************';
%passcode = '**************';
%key = '**********************';
server = 'https://api-public.sandbox.pro.coinbase.com';
%% Prepare for Signature
timestamp = datetime('now','format','yyyy-MM-dd''T''HH:mm:ss''Z');
timestamp = num2str(round(posixtime(timestamp)));
method = 'GET'; %Or POST
requestPath = 'orders';
body = '/users/self/verify';
msg = [timestamp, upper(method), body];
%% Signature
hash256_class = System.Security.Cryptography.SHA256Managed.Create();
challenge_hash = uint8(hash256_class.ComputeHash(uint8(msg)));
secret_uint8 = matlab.net.base64decode(secret);
hmac256_class = System.Security.Cryptography.HMACSHA256(secret_uint8);
sign_uint8 = uint8(hmac256_class.ComputeHash(challenge_hash));
signature = matlab.net.base64encode(sign_uint8);
%% Concat everything together
ApiKey = matlab.net.http.HeaderField('CB-ACCESS-KEY',key);
ApiSignature = matlab.net.http.HeaderField('CB-ACCESS-SIGN',signature);
ApiTimestamp = matlab.net.http.HeaderField('CB-ACCESS-timestamp',timestamp);
ApiPassphrase = matlab.net.http.HeaderField('PASSPHRASE',passcode);
ApiContent = matlab.net.http.HeaderField('Content-Type','application/json');
Header = [ApiKey,ApiSignature,ApiTimestamp,ApiPassphrase,ApiContent];
private_options = weboptions('HeaderFields', Header);
%% Read
newPath = [server '/' requestPath];
req = webwrite(newPath, private_options);
Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray (line 396)
The server returned the status 401 with message "" in response to the request to URL
https://api-public.sandbox.pro.coinbase.com/orders.
Risposte (1)
Sanchari
il 13 Feb 2024
Hello Chris,
The 401 Unauthorized error means that the request has not been applied because it lacks valid authentication credentials for the target resource. Since you are using the Coinbase API, this typically indicates an issue with the API key, secret, passphrase, or the signature generation process.
Here is a checklist to troubleshoot this error:
- Check API Key, Secret, or Passphrase: Ensure that you have correctly entered the API key, secret, and passphrase from the Coinbase Pro API settings. Ensure there are no leading or trailing spaces or other hidden characters.
- Check API permissions: Ensure that you have correct permissions set on the Coinbase Pro API settings page to access the requested resources.
- Use correct Endpoint: Verify usage of correct URL for the Coinbase Pro sandbox environment. Sometimes, the base URL might change, or you might accidentally use the production URL instead of the sandbox one.
- Ensure proper Signature: The signature should be a base64-encoded HMAC-SHA256 hash of the prehash string, which is created by concatenating the timestamp, HTTP method (GET, POST, etc.), the request path, and the body of the request (must be empty for a GET request or the JSON body of the request for POST).
- Correct Timestamp: The timestamp must be in Unix time and represent the current time in UTC. If the timestamp is too far off from the server's time, the request will be rejected.
- Header Field Names: Make sure the header fields are correctly named and match the expected headers for the Coinbase API.
Here is a sample code revised with the above points in consideration:
%% Set up api keys/passwords
secret = 'your_api_secret';
passphrase = 'your_api_passphrase';
key = 'your_api_key';
server = 'https://api-public.sandbox.pro.coinbase.com';
%% Prepare for Signature
timestamp = datetime('now','TimeZone','UTC',’format’,’yyyy-MM-dd’ ‘T’ ‘HH:mm:ss.SSSSSS’ ‘Z’);
timestamp = num2str(posixtime(timestamp));
method = 'GET';
requestPath = '/orders'; % Make sure this is the correct API endpoint
body = ''; % Body should be empty for GET requests
% Create the prehash string to sign
msg = [timestamp method requestPath body];
% Generate the signature
decodedSecret = matlab.net.base64decode(secret);
signature = matlab.net.base64encode(HMAC_SHA256(msg, decodedSecret));
%% Concat everything together
ApiKey = matlab.net.http.HeaderField('CB-ACCESS-KEY', key);
ApiSignature = matlab.net.http.HeaderField('CB-ACCESS-SIGN', signature);
ApiTimestamp = matlab.net.http.HeaderField('CB-ACCESS-TIMESTAMP', timestamp);
ApiPassphrase = matlab.net.http.HeaderField('CB-ACCESS-PASSPHRASE', passphrase);
ApiContent = matlab.net.http.HeaderField('Content-Type', 'application/json');
Header = [ApiKey, ApiSignature, ApiTimestamp, ApiPassphrase, ApiContent]; %% Header array
%% Set web options
private_options = weboptions('HeaderFields', Header, 'Content-Type', 'json');
%% Read
newPath = [server requestPath];
response = webread(newPath, private_options);
In this code, I have used a function HMAC_SHA256 which is assumed to be a function you have defined to generate the HMAC-SHA256 signature. If you have not defined such a function, please replace, and use the appropriate MATLAB functions/ classes to create this signature. Please also replace 'your_api_secret', 'your_api_passphrase', and 'your_api_key' with your actual credentials from Coinbase Pro.
If you are still encountering issues after these corrections, it might be helpful to review the official Coinbase Pro API documentation or contact their support for further assistance.
Please go through the following similar ML Answer case as well for further knowledge on coinbase API connectivity problem: https://www.mathworks.com/matlabcentral/answers/1900645-coinbase-api-connectivity-problem?s_tid=answers_rc1-1_p1_BOTH
I hope this information is helpful to you!
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!