Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frequent 401 Unauthorized Client Errors #19

Closed
camandrewz opened this issue Jan 29, 2024 · 16 comments
Closed

Frequent 401 Unauthorized Client Errors #19

camandrewz opened this issue Jan 29, 2024 · 16 comments

Comments

@camandrewz
Copy link

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.

Copy link

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
Copy link
Contributor

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
Copy link

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 urischwartz-cb reopened this Feb 9, 2024
@urischwartz-cb
Copy link
Contributor

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

@vinooganesh
Copy link

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
Copy link
Contributor

@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
Copy link

@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
Copy link
Contributor

urischwartz-cb commented Feb 9, 2024

@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
Copy link

@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
Copy link
Contributor

urischwartz-cb commented Feb 9, 2024

@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
Copy link

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
Copy link
Contributor

@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
Copy link

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

@roosglades
Copy link

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

@urischwartz-cb
Copy link
Contributor

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants