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

Invalid status code 400 when trying to get account info #14

Open
shashishekhard opened this issue Feb 19, 2025 · 4 comments
Open

Invalid status code 400 when trying to get account info #14

shashishekhard opened this issue Feb 19, 2025 · 4 comments
Assignees

Comments

@shashishekhard
Copy link

I am a newbie and this may be a simple thing. I was not able to get a satisfactory answer anywhere and also code I am getting is for the older sdk which is deprecated, so I am posting it here. I am getting following error when I try to get account information.

/myenv/lib/python3.11/site-packages/kucoin_universal_sdk/internal/infra/default_transport.py", line 184, in process_response raise RestError(None, f"Invalid status code: {resp.status_code}, msg: {resp_body.decode('utf-8')}") kucoin_universal_sdk.model.common.RestError: request error, context msg: Invalid status code: 400, msg: {"code":"400001","msg":"Please check the header of your request for KC-API-KEY, KC-API-SIGN, KC-API-TIMESTAMP, KC-API-PASSPHRASE"}

When we create transport option and Client option with the api key, secret and pass phrase should it not return account info as it has all the required details. My code is as below:

`http_transport_option = (
        TransportOptionBuilder()
        .set_keep_alive(True)
        .set_max_pool_size(10)
        .set_max_connection_per_pool(10)
        .build()
    )
client_option = (
        ClientOptionBuilder()
        .set_key(k_api_key)
        .set_secret(k_api_secret)
        .set_passphrase(k_api_passphrase)
        .set_spot_endpoint(GLOBAL_API_ENDPOINT)
        .set_transport_option(http_transport_option)
        .build()
    )
client = DefaultClient(client_option)

account_api = client.rest_service().get_account_service().get_account_api()
`

Do we need to separately sign and return timestamp for the request?

@ISAAC-XXYYZZ
Copy link
Contributor

Hi, this error indicates that there is no correct key-related information. First, please check if the key information is correct. Your example code is incomplete, and I can’t see specifically which method under account_api was called. Please provide more context, and I’ll help you resolve the issue.

@ISAAC-XXYYZZ ISAAC-XXYYZZ self-assigned this Feb 20, 2025
@shashishekhard
Copy link
Author

shashishekhard commented Feb 20, 2025

Thank you for your response. The code I posted before is picked from the examples. I tried getting the account_api and print the account information. The file name is example_api.py.

https://github.com/Kucoin/kucoin-universal-sdk/blob/main/sdk/python/example/example_api.py (line 73)

Do I need to add the signature and timestamp separately or the api does it for us?

`def account_service_example(account_service: AccountService):
    # Get account api
    account_api = account_service.get_account_api()
    # Get summary information
    account_info_resp = account_api.get_account_info()
    logging.info(f"account info: level:{account_info_resp.level}, SubAccountSize:{account_info_resp.sub_quantity}")
    # Other Account APIs...

    # Get fee api
    fee_api = account_service.get_fee_api()
    get_actual_fee_req = GetSpotActualFeeReqBuilder().set_symbols("BTC-USDT,ETH-USDT").build()
    get_actual_fee_resp = fee_api.get_spot_actual_fee(get_actual_fee_req)
    for fee_data in get_actual_fee_resp.data:
        logging.info(
            f"fee info: symbol:{fee_data.symbol}, TakerFee:{fee_data.taker_fee_rate}, MakerFee:{fee_data.maker_fee_rate}")
    .`

@ISAAC-XXYYZZ
Copy link
Contributor

Hi,

As a client, you only need to set the necessary keys. Other parameters, such as the signature and timestamp you mentioned, don’t need to be set manually.

I ran the code again based on your input and didn’t find any issues. Here’s the output:

(.venv) ➜  py python example_api.py
2025-02-21 09:44:42 INFO - sdk version: v1.1.0
2025-02-21 09:44:43 INFO - account info: level:0, SubAccountSize:3
2025-02-21 09:44:43 INFO - fee info: symbol:BTC-USDT, TakerFee:0.001, MakerFee:0.001
2025-02-21 09:44:43 INFO - fee info: symbol:ETH-USDT, TakerFee:0.001, MakerFee:0.001
(.venv) ➜  py cat example_api.py
import logging
import os
import uuid
from datetime import datetime, timedelta

from kucoin_universal_sdk.api.client import DefaultClient
from kucoin_universal_sdk.generate.account.fee.model_get_spot_actual_fee_req import GetSpotActualFeeReqBuilder
from kucoin_universal_sdk.generate.futures.market.model_get_klines_req import GetKlinesReqBuilder, GetKlinesReq
from kucoin_universal_sdk.generate.service.account_api import AccountService
from kucoin_universal_sdk.generate.service.futures_api import FuturesService
from kucoin_universal_sdk.generate.service.spot_api import SpotService
from kucoin_universal_sdk.generate.spot.order.model_add_order_sync_req import AddOrderSyncReqBuilder, AddOrderSyncReq
from kucoin_universal_sdk.generate.spot.order.model_cancel_order_by_order_id_sync_req import \
    CancelOrderByOrderIdSyncReqBuilder
from kucoin_universal_sdk.generate.spot.order.model_get_order_by_order_id_req import GetOrderByOrderIdReqBuilder
from kucoin_universal_sdk.model.client_option import ClientOptionBuilder
from kucoin_universal_sdk.model.constants import GLOBAL_API_ENDPOINT, GLOBAL_FUTURES_API_ENDPOINT, \
    GLOBAL_BROKER_API_ENDPOINT
from kucoin_universal_sdk.model.transport_option import TransportOptionBuilder


def rest_example():
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s %(levelname)s - %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    )

    #  Retrieve API secret information from environment variables
    key = os.getenv("API_KEY", "")
    secret = os.getenv("API_SECRET", "")
    passphrase = os.getenv("API_PASSPHRASE", "")

    # Optional: Retrieve broker secret information from environment variables; applicable for broker API only
    broker_name = os.getenv("BROKER_NAME")
    broker_key = os.getenv("BROKER_KEY")
    broker_partner = os.getenv("BROKER_PARTNER")

    # Set specific options, others will fall back to default values
    http_transport_option = (
        TransportOptionBuilder()
        .set_keep_alive(True)
        .set_max_pool_size(10)
        .set_max_connection_per_pool(10)
        .build()
    )

    # Create a client using the specified options
    client_option = (
        ClientOptionBuilder()
        .set_key(key)
        .set_secret(secret)
        .set_passphrase(passphrase)
        .set_broker_name(broker_name)
        .set_broker_key(broker_key)
        .set_broker_partner(broker_partner)
        .set_spot_endpoint(GLOBAL_API_ENDPOINT)
        .set_futures_endpoint(GLOBAL_FUTURES_API_ENDPOINT)
        .set_broker_endpoint(GLOBAL_BROKER_API_ENDPOINT)
        .set_transport_option(http_transport_option)
        .build()
    )
    client = DefaultClient(client_option)

    # Get the Restful Service
    kucoin_rest_service = client.rest_service()

    account_service_example(kucoin_rest_service.get_account_service())


def account_service_example(account_service: AccountService):
    # Get account api
    account_api = account_service.get_account_api()
    # Get summary information
    account_info_resp = account_api.get_account_info()
    logging.info(f"account info: level:{account_info_resp.level}, SubAccountSize:{account_info_resp.sub_quantity}")
    # Other Account APIs...

    # Get fee api
    fee_api = account_service.get_fee_api()
    get_actual_fee_req = GetSpotActualFeeReqBuilder().set_symbols("BTC-USDT,ETH-USDT").build()
    get_actual_fee_resp = fee_api.get_spot_actual_fee(get_actual_fee_req)
    for fee_data in get_actual_fee_resp.data:
        logging.info(
            f"fee info: symbol:{fee_data.symbol}, TakerFee:{fee_data.taker_fee_rate}, MakerFee:{fee_data.maker_fee_rate}")
    # Other Fee APIs...

    #  Other APIs related to account...

if __name__ == "__main__":
    rest_example()
(.venv) ➜  py

I have my keys set in environment variables like this:

API_KEY=*****
API_SECRET=*****
API_PASSPHRASE=*****

Your issue is most likely due to incorrect key/secret/passphrase configuration. Please check them on the KuCoin official website.

If you still encounter any issues, please let me know promptly.

@shashishekhard
Copy link
Author

shashishekhard commented Feb 21, 2025

I verified my api key and secret and have tried accessing data from kucoin. I copied the program as is and then updated my api keys and tried to execute the code. I am still getting same issue. I am able to create spot service and get the ohclv data, get trade history etc.

But I did notice one more thing.. It saying API token is empty does this have anything to do with the api keys ?

`2025-02-21 20:09:14 INFO - sdk version: v1.1.0
2025-02-21 20:09:14 WARNING - API token is empty. Access is restricted to public interfaces only.
Traceback (most recent call last):
File "/home/sdeshpande/pyprograms/example.py", line 91, in
rest_example()
File "/home/sdeshpande/pyprograms/example.py", line 68, in rest_example
account_service_example(kucoin_rest_service.get_account_service())
File "/home/sdeshpande/pyprograms/example.py", line 75, in account_service_example
account_info_resp = account_api.get_account_info()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sdeshpande/myenv/lib/python3.11/site-packages/kucoin_universal_sdk/generate/account/account/api_account.py", line 332, in get_account_info
return self.transport.call("spot", False, "GET", "/api/v2/user-info",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sdeshpande/myenv/lib/python3.11/site-packages/kucoin_universal_sdk/internal/infra/default_transport.py", line 232, in call
return self.process_response(response_body, response_obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sdeshpande/myenv/lib/python3.11/site-packages/kucoin_universal_sdk/internal/infra/default_transport.py", line 184, in process_response
raise RestError(None, f"Invalid status code: {resp.status_code}, msg: {resp_body.decode('utf-8')}")
kucoin_universal_sdk.model.common.RestError: request error, context msg: Invalid status code: 400, msg: {"code":"400001","msg":"Please check the header of your request for KC-API-KEY, KC-API-SIGN, KC-API-TIMESTAMP, KC-API-PASSPHRASE"}

Rest of the error is same.

I am assuming I dont need broker information which is present in the above code. I have removed that part.

Can you please guide me on this?

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

No branches or pull requests

2 participants