ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
31.63k stars 7.33k forks source link

Coinbase Advanced Trader 401 Unauthorized Issue #22182

Closed tazengineering closed 1 week ago

tazengineering commented 1 month ago

Operating System

Windows

Programming Languages

Python

CCXT Version

4.2.64

Description

Issue when attempting to connect to Coinbase Advanced Trade.

Error: AuthenticationError: coinbase GET https://api.coinbase.com/api/v3/brokerage/products 401 Unauthorized Unauthorized

I have tried with both apiKey and Secret simplified or the full text shown when generating the API.

Code

import ccxt

# Connects to Binance
exchange = ccxt.coinbase({
    'apiKey': "organizations/XXXXXXXXXXXXXXXXXXXX/apiKeys/XXXXXXXXXXXXXXXXXXXXX",
    'secret': "-----BEGIN EC PRIVATE KEY-----\nXXXXXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//XXXXX/XXXXXXXXXXXXXX\nXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==\n-----END EC PRIVATE KEY-----\n",
})

# Define lot sizes for each symbol
lot_sizes = {'BTC/USDT:USDT': 0.001,
             'ETH/USDT:USDT': 0.01,
             'DOGE/USDT:USDT': 100,
             'BNB/USDT:USDT': 0.01,
             'ADA/USDT:USDT': 10,
             'EGLD/USDT:USDT': 0.01,
             'MATIC/USDT:USDT': 10,
             'TRX/USDT:USDT': 100,
             'LTC/USDT:USDT': 0.1,
             'SOL/USDT:USDT': 0.1}

# Get the account balance
balance = exchange.fetch_balance()
total_funds = float(balance['total']['USDT'])
#print(balance['info'])
print(total_funds)
carlosmiei commented 1 month ago

Hello @tazengineering, cloud keys support was introduced recently, so you need to update your ccxt version to get it

tazengineering commented 1 month ago

Hi @carlosmiei

I have updated to ccxt-4.2.100 and the issue persists.

carlosmiei commented 1 month ago

@tazengineering Can you please double-check your api keys/permissions/ip restrictions/etc? I'm using it right now and seems to be working

tazengineering commented 1 month ago

@carlosmiei

Thank you for taking the time to support me.

Are you using the same format for API key and secret I shared?

I removed completely any IP restrictions.

Where do you setup permissions from?

Regards Ciprian

carlosmiei commented 1 month ago

@tazengineering I'm using the keys from the file coinbase provides upon creating the keys

image

And I'm creating them here: https://portal.cloud.coinbase.com/access/api?keyType=trade&referrer=advanced_trade

KRV1 commented 1 month ago

Guys, sorry, must the secret key set as parameter "as is"? I'm getting in PHP (version 4.2.100) in BigInteger.php error, because secret key transmitted to it as is - with "BEGIN PRIVATE KEY" etc.

carlosmiei commented 1 month ago

@KRV1 Are you reading the keys from a file? Can you check if the \n are properly escaped?

carlosmiei commented 1 month ago

@KRV1 Can you try to parse your secret doing this?

$private_key = str_replace('\n', "\n", $private_key);
KRV1 commented 1 month ago

@carlosmiei Thank you, yes problem was in the carriage return symbol Seems it works now

tazengineering commented 1 month ago

Thank you @carlosmiei

Unfortunately I could not manage to make it work in Python.

I see you have yours using .json and split into multiple keys - where I only have 'apiKey' and 'secret' key.

exchange = ccxt.coinbase({
    'apiKey': "organizations/XXXXXXXXXXXXXXXXXXXX/apiKeys/XXXXXXXXXXXXXXXXXXXXXXXXXX",
    'secret': "-----BEGIN EC PRIVATE KEY-----\nXXXXXXXXXXXXX==XXXXXXXXXXXXXXXXXXX==XXXXX\nXXXXXXXXXXXXXXXXXXXXXXX==XXXXXXXXXXXXXX\nXXXXXXXXX==XXXXXXXXXX==XXXXXXXXXXXXXXXX==XXXXXXXXXX==\n-----END EC PRIVATE KEY-----\n",
})
bradym05 commented 1 month ago

issue is persisting for me

reverendrenegade commented 3 weeks ago

I am having the same issue as well. There doesn't seem to be anything conclusive anywhere I've looked. This seems to be a CB issue because there are many questions about it

bradym05 commented 3 weeks ago

I thought this was a CB issue as well so I decided to try using the CB advanced trade SDK. It works perfectly so I'm no longer using ccxt, hopefully this gets fixed.

carlosmiei commented 3 weeks ago

@reverendrenegade @bradym05 This is tricky because as I said locally things work as expected for us. What's your ccxt and python version?

carlosmiei commented 3 weeks ago

@bradym05 @bradym05 I can try to ask coinbase directly about your specific issue, can you share your accountId/email via pm in discord/telegram?

reverendrenegade commented 3 weeks ago

@carlosmiei @bradym05

Thanks for the reply!

ccxt Version: 4.3.18 Python Version: 3.11.2 (main, May 8 2024, 11:29:38) [GCC 9.4.0]

After updating to the latest version of ccxt and Python this solved the issue. Thanks for everyone's time.

carlosmiei commented 3 weeks ago

@reverendrenegade Great, thanks for letting us know!

jtashiro commented 2 weeks ago

Upgraded to 4.3.27 to use the Coinbase CDP API keys, now am getting these errors in all python scripts on any API call. No other changes were made. Is there some parameter that I need to pass to get backward compatibility??

CCXT Version: 4.3.27 index out of range

EDIT: Figured out that I needed to convert the \n chars in the private key into actual newline in python code.

private_key = src_private_key.replace('\\n', '\n').replace('\\t', '\t')

That resolved it.

theMoe commented 1 week ago

CCXT Version: 4.3.29 Python: 3.12.3

I tried the following, but I still get the issue: IndexError: index out of range

config['coinbase_secret'].replace('\n', "\n")

and

config['coinbase_secret'].replace('\n', "\n").replace('\t', '\t')

Can someone help me?

I use this structure, with a .env file:

exchange = ccxt.coinbase({
    'apiKey': "organizations/XXXXXXXXXXXXXXXXXXXX/apiKeys/XXXXXXXXXXXXXXXXXXXXXXXXXX",
    'secret': "-----BEGIN EC PRIVATE KEY-----\nXXXXXXXXXXXXX==XXXXXXXXXXXXXXXXXXX==XXXXX\nXXXXXXXXXXXXXXXXXXXXXXX==XXXXXXXXXXXXXX\nXXXXXXXXX==XXXXXXXXXX==XXXXXXXXXXXXXXXX==XXXXXXXXXX==\n-----END EC PRIVATE KEY-----\n",
})
jtashiro commented 1 week ago

You’ll need 2 backslashes in the first parameter replace() method.

config['coinbase_secret'].replace(‘\n', "\n")

and

config['coinbase_secret'].replace(‘\n', "\n").replace(‘\t', '\t')

theMoe commented 1 week ago

Awesome, thanks for your fast support!

jtashiro commented 1 week ago

Awesome, thanks for your fast support!

I happen to be in the middle of fixing my scripts for Coinbase CDP and ccxt upgrades ... hope this helps you out.

carlosmiei commented 1 week ago

@jtashiro Thanks for the help! 🙂

ttodua commented 1 week ago

as there remains no users in this thread (who experiences issues) i'll close this topic. feel free to contact back again if any issue