Click here to Skip to main content
15,068,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm starting to use PayPal for payments with .net Core. I created a sandbox app and checked client id and secret. I get an error at
content


"{"error":"invalid_client","error_description":"Client Authentication failed"}"


Code:
private async Task<PayPalAccessToken> GetPayPalAccessTokenAsync(HttpClient httpClient)
    {
        byte[] bytes = Encoding.GetEncoding("iso-8859-1")
            .GetBytes($"{_configuration["PayPal:clientId"]} : {_configuration["PayPal:secret"]}");

        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "/v1/oauth2/token");
        requestMessage.Headers.Authorization =
            new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytes));

        var form = new Dictionary<string, string>
        {
            ["grant_type"] = "client_credentials"
        };

        requestMessage.Content = new FormUrlEncodedContent(form);

        HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);

        string content = await responseMessage.Content.ReadAsStringAsync();
        PayPalAccessToken accessToken = JsonConvert.DeserializeObject<PayPalAccessToken>(content);
        return accessToken;
    }


Setting:
"PayPal": {
    "clientId": "xxx",
    "secret": "xxx",
    "urlAPI": "https://api-m.sandbox.paypal.com",
    "returnUrl": "https://localhost:44370/cart/success",
    "cancelUrl": "https://localhost:44370/cart/cancel"
}


And here is Code I followed
Paypal .net core C# sample · GitHub[^]

Full my code hastebin[^]

What is the problem that I am facing?

What I have tried:

I tried creating a new sandbox app and assigning client id and secret to it but still the same.
Posted
Updated 28-Jul-21 23:13pm

I would suggest you contact paypal. You are either not including something, like a certificate, or the credentials you have are wrong.
   
Quote:
C#
$"{_configuration["PayPal:clientId"]} : {_configuration["PayPal:secret"]}"
You're adding a space to the end of your client ID, and a space at the start of your secret.

Instead of sending:
xxx:xxx
you're sending:
xxx : xxx
Remove the spaces from your interpolated string:
C#
byte[] bytes = Encoding.GetEncoding("iso-8859-1")
    .GetBytes($"{_configuration["PayPal:clientId"]}:{_configuration["PayPal:secret"]}");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900