coinbase / coinbase-advanced-py

The Advanced API Python SDK is a Python package that makes it easy to interact with the Coinbase Advanced API. The SDK handles authentication, HTTP connections, and provides helpful methods for interacting with the API.
https://docs.cdp.coinbase.com/advanced-trade/docs/welcome/
Apache License 2.0
101 stars 27 forks source link

Frequent 401 Unauthorized Client Errors #19

Closed camandrewz closed 6 months ago

camandrewz commented 7 months ago

When attempting to use the SDK by calling any of the rest functions ("get_products" for example), sometimes the correct response will be returned, but usually a HTTPError exception occurs with the following: "401 Client Error: Unauthorized Unauthorized". I'm using the proper API keys and the issue seems to be server side with the inconsistent responses.

github-actions[bot] commented 7 months ago

Thank you for reporting! If this is an SDK specific issue, we will look into it and get back to you soon. If this is an API related request, report it in our Advanced API forum instead.

urischwartz-cb commented 7 months ago

Hi @camandrewz, thanks for flagging. Has this been resolved? We were able to see you successfully making requests throughout the day. If not you can create a support ticket here and we can help you more directly.

vinooganesh commented 7 months ago

Hi @urischwartz-cb -I'm getting this issue fairly frequently and sporadically as well. Is there any guidance to resolve this? The responses are unquestionably inconsistent. Meaning, I'll get a working response, make no code changes, run it again and see a failure.

Not working:

2024-02-08 22:16:46 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com/
2024-02-08 22:16:46 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-08 22:16:46 - coinbase.WSClient - DEBUG - Successfully subscribed
{"type":"error","message":"authentication failure"}
2024-02-08 22:16:51 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-08 22:16:51 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-08 22:16:51 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-08 22:16:51 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-08 22:16:51 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)

No code change. Working:

2024-02-08 22:17:49 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com/
2024-02-08 22:17:49 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-08 22:17:49 - coinbase.WSClient - DEBUG - Successfully subscribed
{"channel":"ticker","client_id":"","timestamp":"2024-02-09T03:17:49.000088799Z","sequence_num":0,"events":[{"type":"snapshot","tickers":[{"type":"ticker","product_id":"BTC-USD","price":"46221.86","volume_24_h":"19480.95913896","low_24_h":"44419.36","high_24_h":"46395.33","low_52_w":"19568.52","high_52_w":"49102.29","price_percent_chg_24_h":"3.45659138797705"}]}]}

{"channel":"subscriptions","client_id":"","timestamp":"2024-02-09T03:17:49.000106727Z","sequence_num":1,"events":[{"subscriptions":{"ticker":["BTC-USD"]}}]}
{"channel":"ticker","client_id":"","timestamp":"2024-02-09T03:17:49.020993024Z","sequence_num":2,"events":[{"type":"update","tickers":[{"type":"ticker","product_id":"BTC-USD","price":"46223.75","volume_24_h":"19480.95913896","low_24_h":"44419.36","high_24_h":"46395.33","low_52_w":"19568.52","high_52_w":"49102.29","price_percent_chg_24_h":"3.46082170146342"}]}]}
...

@urischwartz-cb what information can I get you to help you debug? This almost looks like a rate limit of some sort.

urischwartz-cb commented 7 months ago

Thank you for flagging! We will prioritize fixing this and get back to you if we need any information to help debug.

vinooganesh commented 7 months ago

Thanks! Out of curiosity, do you have any thoughts about what could be causing it? I'm wondering if I should move back to the regular rest endpoints for the time being, especially if the investigation may take some time.

urischwartz-cb commented 7 months ago

@vinooganesh we just pushed something that will help us identify the source of the unauth issues more clearly. Please retry using the WebSocket client and let me know timestamps of when the error occurred. Thank you!

vinooganesh commented 7 months ago

@urischwartz-cb sure, here you go:

Not working:

2024-02-09 11:35:12 - coinbase.WSClient - DEBUG - Connecting to wss://advanced-trade-ws.coinbase.com
2024-02-09 11:35:12 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com/
2024-02-09 11:35:12 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-09 11:35:12 - coinbase.WSClient - DEBUG - Successfully subscribed
{"type":"error","message":"authentication failure"}
2024-02-09 11:35:17 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-09 11:35:17 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-09 11:35:17 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-09 11:35:17 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-09 11:35:17 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)

Working:

2024-02-09 11:35:23 - coinbase.WSClient - DEBUG - Connecting to wss://advanced-trade-ws.coinbase.com
2024-02-09 11:35:23 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com/
2024-02-09 11:35:23 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-09 11:35:23 - coinbase.WSClient - DEBUG - Successfully subscribed
{"channel":"ticker","client_id":"","timestamp":"2024-02-09T16:35:23.078573291Z","sequence_num":0,"events":[{"type":"snapshot","tickers":[{"type":"ticker","product_id":"BTC-USD","price":"47235.08","volume_24_h":"20396.14292071","low_24_h":"44893.21","high_24_h":"47723.78","low_52_w":"19568.52","high_52_w":"49102.29","price_percent_chg_24_h":"4.52438112532084"}]}]}
...
2024-02-09 11:35:28 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-09 11:35:28 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-09 11:35:28 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-09 11:35:28 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-09 11:35:28 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)

Code:


from coinbase.websocket import (WSClient, WSClientConnectionClosedException,
                                WSClientException)
import time
ws_client = WSClient(key_file="./coinbase-sdk/coinbase_cloud_api_key.json", on_message=on_message, verbose=True)
try:
    ws_client.open()
    ws_client.ticker(product_ids=["BTC-USD"])
    time.sleep(5)
    ws_client.ticker_unsubscribe(product_ids=["BTC-USD"])
    ws_client.close()
except WSClientConnectionClosedException as e:
    print("Connection closed! Retry attempts exhausted.")
except WSClientException as e:
    print("Error encountered!")
urischwartz-cb commented 7 months ago

@vinooganesh we believe the issue stems from clock skew, since your request JWTs seem to have an expiry time that is greater than 2 minutes past when we receive your request. Could you compare the output of your mahcine's time.time() vs hitting https://api.coinbase.com/v2/time please? Particularly in an example where the authentication fails. We would expect some divergence there.

vinooganesh commented 7 months ago

@urischwartz-cb, sure here you go

2024-02-09 13:40:42 - coinbase.WSClient - DEBUG - Connecting to wss://advanced-trade-ws.coinbase.com
2024-02-09 13:40:43 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com/
2024-02-09 13:40:43 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-09 13:40:43 - coinbase.WSClient - DEBUG - Successfully subscribed
time.time(): 1707504042.871829
Coinbase API time: {"data":{"iso":"2024-02-09T18:40:42Z","epoch":1707504042}}
{"type":"error","message":"authentication failure"}
2024-02-09 13:40:46 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-09 13:40:46 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-09 13:40:46 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-09 13:40:46 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-09 13:40:46 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)

Updated code:

    print("time.time(): " + str(time.time()))
    print("Coinbase API time: " + requests.get("https://api.coinbase.com/v2/time").text)
    ws_client.open()
    ws_client.ticker(product_ids=["BTC-USD"])
urischwartz-cb commented 7 months ago

@vinooganesh we just pushed a change that will allow us to confirm the expiry time you are sending us in the failed requests. Please let me know once you are able to repro again and I will check this. Thanks again for your patience as we work to figure this out.

vinooganesh commented 7 months ago

No problem @urischwartz-cb, here you go (Reproed a few times):

2024-02-09 16:39:00 - coinbase.WSClient - DEBUG - Connecting to wss://advanced-trade-ws.coinbase.com
time.time(): 1707514740.449631
Coinbase API time: {"data":{"iso":"2024-02-09T21:38:59Z","epoch":1707514739}}
2024-02-09 16:39:00 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com
2024-02-09 16:39:00 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-09 16:39:00 - coinbase.WSClient - DEBUG - Successfully subscribed
{"type":"error","message":"authentication failure"}
2024-02-09 16:39:03 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-09 16:39:03 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-09 16:39:03 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-09 16:39:03 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-09 16:39:03 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)
2024-02-09 16:39:24 - coinbase.WSClient - DEBUG - Connecting to wss://advanced-trade-ws.coinbase.com
time.time(): 1707514763.8576
Coinbase API time: {"data":{"iso":"2024-02-09T21:39:23Z","epoch":1707514763}}
2024-02-09 16:39:24 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com
2024-02-09 16:39:24 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-09 16:39:24 - coinbase.WSClient - DEBUG - Successfully subscribed
{"type":"error","message":"authentication failure"}
2024-02-09 16:39:27 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-09 16:39:27 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-09 16:39:27 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-09 16:39:27 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-09 16:39:27 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)
2024-02-09 16:39:42 - coinbase.WSClient - DEBUG - Connecting to wss://advanced-trade-ws.coinbase.com
2024-02-09 16:39:42 - coinbase.WSClient - DEBUG - Successfully connected to wss://advanced-trade-ws.coinbase.com/
2024-02-09 16:39:42 - coinbase.WSClient - DEBUG - Subscribing to channel ticker for product IDs: ['BTC-USD']
2024-02-09 16:39:42 - coinbase.WSClient - DEBUG - Successfully subscribed
time.time(): 1707514781.932235
Coinbase API time: {"data":{"iso":"2024-02-09T21:39:41Z","epoch":1707514781}}
{"type":"error","message":"authentication failure"}
2024-02-09 16:39:45 - coinbase.WSClient - DEBUG - Unsubscribing from channel ticker for product IDs: ['BTC-USD']
2024-02-09 16:39:45 - coinbase.WSClient - DEBUG - Successfully unsubscribed
2024-02-09 16:39:45 - coinbase.WSClient - DEBUG - Closing connection to wss://advanced-trade-ws.coinbase.com
2024-02-09 16:39:45 - coinbase.WSClient - DEBUG - Connection closed to wss://advanced-trade-ws.coinbase.com/
2024-02-09 16:39:45 - coinbase.WSClient - DEBUG - Connection closed (OK): sent 1000 (OK); then received 1000 (OK)
urischwartz-cb commented 7 months ago

@vinooganesh we just pushed a change that should solve the problem for the WSClient. It does seem to have been a clock skew issue. Please let me know if it is resolved. We will push out a similar fix for REST connections on Monday. Thanks for your feedback and for helping us improve our product!

vinooganesh commented 7 months ago

@urischwartz-cb - Thanks for all of your help here! It looks good from my end now.

roosglades commented 6 months ago

were fixes committed to this issue? I am experiencing the same problem

urischwartz-cb commented 6 months ago

@roosglades We fixed the issue in the WebSocket service. A fix for the REST API is in our release queue. Are you still seeing issues with WebSocket or only REST?

roosglades commented 6 months ago

I was getting them for REST. I updated to 1.1.3 and it seemed to resolved the issue.