confessore / CoinbaseAT

The unofficial .NET/C# client library for the Coinbase Advanced Trade API
MIT License
2 stars 1 forks source link

Authentication Issue #5

Open stephanie-rimel opened 2 months ago

stephanie-rimel commented 2 months ago

So i am trying to use this is a Blazor Server .Net 8 Project and am having an issue getting the authentication to work. Everytime i try to submit the trade, i receive the Unauthorized response.

I am registering the service like this in my Program.cs var configuration = new CoinbaseATConfiguration(

    apiKey, apiSecret
);

builder.Services.AddSingleton<ICoinbaseATConfiguration>(configuration);
builder.Services.AddSingleton<ICoinbaseATClient, CoinbaseATClient>();
 // builder.Services.AddSingleton<CoinbaseATClient>();

I am injecting the CoinbaseAtClient like this in my .Razor page:

@inject ICoinbaseATClient CoinbaseATClient

@code {
// ...
var result = await AtService.CreatMarketOrderAsync(Guid.NewGuid().ToString(), crypto.ProductId, OrderSide.BUY.ToString(), order_configuration);
var acct = await CoinbaseATClient.ProductsService.GetProductAsync("BTC-USD");
}

and here is my exception ---> System.Exception: Exception of type 'System.Exception' was thrown. --- End of inner exception stack trace --- at CoinbaseAT.Services.Abstractions.CoinbaseATService.SendHttpRequestMessageAsync(HttpMethod httpMethod, String requestPath, String contentBody) at CoinbaseAT.Services.Abstractions.CoinbaseATService.SendServiceCall[T](HttpMethod httpMethod, String requestPath, String contentBody) at CoinbaseAT.Services.ProductsService.GetProductAsync(String product_id) at BotTrading.Shared.CryptoComponent.CryptoComponent.SubmitPosition(String orderType, OrderSide orderSide) in C:\Users\Stephanie\source\repos\BotTrading\BotTrading\Shared\CryptoComponent\CryptoComponent.razor:line 123

When i try to query an endpoint that doesnt require authentication i get this error: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.HttpIOException: The response ended prematurely. (ResponseEnded) at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) at CoinbaseAT.Services.Abstractions.CoinbaseATService.SendHttpRequestMessageAsync(HttpMethod httpMethod, String requestPath, String contentBody) at CoinbaseAT.Services.Abstractions.CoinbaseATService.SendServiceCall[T](HttpMethod httpMethod, String requestPath, String contentBody) at CoinbaseAT.Services.ProductsService.GetProductAsync(String product_id) at BotTrading.Shared.CryptoComponent.CryptoComponent.SubmitPosition(String orderType, OrderSide orderSide) in C:\Users\Stephanie\source\repos\BotTrading\BotTrading\Shared\CryptoComponent\CryptoComponent.razor:line

I have already verified that my apiKey and apiSecret are correct and that they are being passeed into my CoinbaseATConfiguration, I am able to do api calls with these same credentials with another library package. I cant figure out what is causing this. I have the same problem in both the razor file and in the service file.

I even tried it with the ICoinbaseAtClient for good measure, and tried registering the CoinbaseAtClient independntly as well in my Program.cs to no avail. Do you have any hints on what could resolve this issue? I really love the project and how thourough it is i do not want to remove it because nothing else is this complete. Very nice work, and thank you for any help you can offer!

confessore commented 2 months ago

recently i have not shown this project the love it deserves. i'll take a look in this direction when i get a moment. i have to finish clocking in my hours first. 😎

looks like this is what i need to check out when i get a chance

image

looks like there's a AT Authentication image image

# Example request to get account
curl -H "Authorization: Bearer $JWT" 'https://api.coinbase.com/api/v3/brokerage/accounts/f603f97c-37d7-4e58-b264-c27e9e393dd9'
stephanie-rimel commented 2 months ago

you're amazing cant believe how fast you were! looking forward to hearing what you come up with.

confessore commented 2 months ago

😊 thanks. i can get to this after thursday

@barrywood78 did this already here: https://github.com/barrywood78/Coinbase.AdvancedTrade/blob/main/Coinbase.AdvancedTrade/JwtTokenGenerator.cs

so basically what i have learned is that the CoinbasePro endpoints are still functional and may be reached with the legacy headers however CoinbaseAdvancedTrade endpoints may or may not require authentication.

if a v3 endpoint requires authentication, it needs a jwt.

i've tried to implement this with no success thus far, I also receiving Unauthorized when the authentication header holds the bearer token.

confessore commented 2 months ago

i'll get this rolled out as soon as possible. considering dropping support for legacy keys 🤷 it turns out i was having ip whitelisting issues. for anyone's information, the ip whitelist field when generating api keys can be left blank to impose no restrictions image image

confessore commented 2 months ago

@stephanie-rimel give v1.1.4 on nuget a go