JKorf / Kucoin.Net

A C# .netstandard client library for the Kucoin REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Kucoin.Net/
MIT License
113 stars 61 forks source link

Error 400004: Invalid KC-API-PASSPHRASE #154

Closed wilsonsuryajaya closed 2 years ago

wilsonsuryajaya commented 2 years ago

Hi there. First of all, thanks for the wonderful library.

I encountered the following issue: "400004: Invalid KC-API-PASSPHRASE" when testing the REST-API on the sandbox environment, while the Socket Client that uses the same credential doesn't. This is the testing code


static async Task Main( string[] args )
{
    // --- The REST API fails with 400004: Invalid KC-API-PASSPHRASE ---
    var kucoinClient = new KucoinClient( new KucoinClientOptions()
    {
        ApiCredentials = new KucoinApiCredentials( Properties.Resources.API_KEY, Properties.Resources.API_SECRET, Properties.Resources.API_PASSPHRASE ),
        LogLevel = LogLevel.Trace,
        RequestTimeout = TimeSpan.FromSeconds( 60 ),
        FuturesApiOptions = new KucoinRestApiClientOptions
        {
            ApiCredentials = new KucoinApiCredentials( Properties.Resources.FUTURE_API_KEY, Properties.Resources.FUTURE_API_SECRET, Properties.Resources.FUTURE_API_PASSPHRASE ),
            AutoTimestamp = false,
            BaseAddress = KucoinApiAddresses.TestNet.FuturesAddress
        }
    } );

    var accounts = await kucoinClient.FuturesApi.Account.GetAccountOverviewAsync();
    if ( !accounts.Success )
        Console.WriteLine( "Request failed: " + accounts.Error );
    else
        Console.WriteLine( "Result: " + accounts.Data );
    // --- End of REST API test ---

    // --- The SocketClient however is able to subscribe and get the market data ---
    var kucoinSocketClient = new KucoinSocketClient( new KucoinSocketClientOptions()
    {
        ApiCredentials = new KucoinApiCredentials( Properties.Resources.API_KEY, Properties.Resources.API_SECRET, Properties.Resources.API_PASSPHRASE ),
        LogLevel = LogLevel.None,
        FuturesStreamsOptions = new KucoinSocketApiClientOptions
        {
            ApiCredentials = new KucoinApiCredentials( Properties.Resources.FUTURE_API_KEY, Properties.Resources.FUTURE_API_SECRET, Properties.Resources.FUTURE_API_PASSPHRASE ),
            BaseAddress = KucoinApiAddresses.TestNet.FuturesAddress
        }
    } );

    var cts = new CancellationTokenSource();
    var subscriptionResult = await kucoinSocketClient.SpotStreams.SubscribeToTickerUpdatesAsync( "BTC-USDT", DataHandler, cts.Token );

    if ( !subscriptionResult.Success )
    {
        Console.WriteLine( "Failed to connect: " + subscriptionResult.Error );
        return;
    }

    Console.ReadLine();
    cts.Cancel();
}

private static void DataHandler( DataEvent<KucoinStreamTick> updateData )
{
    Console.WriteLine( $"{updateData.Data.Sequence},{updateData.Timestamp.ToString( "dd/MM/yyyy HH:mm:ss.fff" )},{updateData.Data.Symbol},{updateData.Data.LastPrice},{updateData.Data.LastQuantity}" );
}

Below output shows that the REST API fails to get the account overview, but the Socket Client is able to subscribe and receive real-time market data using the same credentials.

image

Would you have any clue what might be the cause of this issue?

Thank you

JKorf commented 2 years ago

Hi, the websocket for market data doesn't require any credentials, so it makes sense that it can connect without issue.

I assume you double checked the credentials? The error message indicates an issue with those. Also, you might need seperate credentials for the futures API (can't remember from the top of my head if this is the case for Kucoin), so it might be that you're trying to use your real credentials for the sandbox environment.

wilsonsuryajaya commented 2 years ago

Thanks for the prompt answer.

I created an API key and another separate Future API key for the sandbox environment as shown below (I have just started so I haven't generated any keys for live yet).

image

I tried to search around for people reporting the same issues and tick the following boxes:

but still getting the same issues.

wilsonsuryajaya commented 2 years ago

Hi I don't know what I did wrong previously but today I tried creating new APIs with simple passphrase and it is working now. Thanks for the help anyway.