簡體   English   中英

拋出了“Braintree.Exceptions.AuthenticationException”類型的異常,即使 API 密鑰有效-.Net Core 6

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

我正在嘗試使用沙盒環境,但由於某種原因我似乎無法進行身份驗證,

這里的方法:

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!;
        }
    }

這個 API 密鑰是有效的,如下圖所示,取自我的沙盒帳戶:

點擊查看圖片

在 try & catch 塊中,這是我捕獲的異常:

引發了“Braintree.Exceptions.AuthenticationException”類型的異常。

但是,當我使用時(用於顯示錯誤的更好方法的圖像)

ClientId 錯誤

我應該怎么做才能解決這個問題?

編輯:

異常.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

堆棧跟蹤->

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```

我檢查了您的第二張照片,發現錯誤消息是:

構建braintreegateway時缺少clientid

然后我檢查了有關BraintreeGateway的源代碼。 我發現IBraintreeGateway接口包括Configuration

所以問題是錯誤信息的字面意思,請查收。

答案是,在 Environment.Developement 上運行測試首先,它將允許您提出沙盒請求,在文檔中找不到此請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM