Search code examples
c#asp.net-corebraintreebraintree-sandbox

Exception of type 'Braintree.Exceptions.AuthenticationException' was thrown, even thought API key is valid - .Net Core 6


I'm trying to use the Sandbox Environment, but I can't seem to authenticate for some reason,

Here the method:

public Customer CreateCustomer(
        User user)
    {
        var request = new CustomerRequest
        {
            FirstName = user.Firstname,
            LastName = user.Lastname,
            Email = user.Email
        };

        try{
            var gateway = new BraintreeGateway
              {
               Environment = Braintree.Environment.SANDBOX,
               MerchantId  = "rmzzy8txvc2w35pg",
               PublicKey   = "wqgg52tmkhsfmn9t",
               PrivateKey  = "86ab47514a195d393db6d1b12350facf"
              };

            Braintree.Result<Customer> result = gateway.Customer.Create(request);

            return result.Target;

        }catch(Exception ex){
            Console.WriteLine(ex.Message);

            return null!;
        }
    }

This API key is valid, as shown In the image below taken from my sandbox account:

Click to see Image

In the try & catch block, this is the exception that I catch:

Exception of type 'Braintree.Exceptions.AuthenticationException' was thrown.

However when I Use(Image used for lack of better way of showing the error)

ClientId Error

What should I Do to fix this?

EDIT:

exception.toString :

Braintree.Exceptions.AuthenticationException: Exception of type 'Braintree.Exceptions.AuthenticationException' was thrown.\r\n   at Braintree.HttpService.ThrowExceptionIfErrorStatusCode(HttpStatusCode httpStatusCode, String message)\r\n   at Braintree.HttpService.GetHttpResponseAsync(HttpRequestMessage request)\r\n   at Braintree.BraintreeService.GetXmlResponseAsync(String URL, String method, Request requestBody, FileStream file)\r\n   at Braintree.CustomerGateway.FindAsync(String Id)\r\n   at DedicatedServers_API.Services.BraintreeServices.BraintreeCustomerService.FindCustomer(String customerId) in BraintreeCustomerService.cs:line 95

Stacktrace ->

Exception thrown: 'Braintree.Exceptions.AuthenticationException' in System.Private.CoreLib.dll
   at Braintree.HttpService.ThrowExceptionIfErrorStatusCode(HttpStatusCode httpStatusCode, String message)
   at Braintree.HttpService.<GetHttpResponseAsync>d__21.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Braintree.BraintreeService.<GetXmlResponseAsync>d__18.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Braintree.CustomerGateway.<FindAsync>d__5.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at DedicatedServers_API.Services.BraintreeServices.BraintreeCustomerService.<FindCustomer>d__8.MoveNext() in BraintreeCustomerService.cs:line 95```

Solution

  • AND THE ANSWER IS, RUN A TEST ON Environment.Developement First, than it will allow you to make Sandbox request, nowhere to be found on docs this one.