apipaypalelixirpaypal-sandboxhttpoison

Invalid Client with paypal api, client authentication failed using HTTPoison.post!/3


I am using HTTPoison to send request to the Paypal api. Here is the paypal documentation for using its api for logging in: https://developer.paypal.com/docs/log-in-with-paypal/integrate/

When I get the code, and try to exchange it for an access token, I get this error: "{\"error\":\"invalid_client\",\"error_description\":\"Client Authentication failed\"}",

Here is how HTTPoison.post!/3 post request:

url = "https://api-m.sandbox.paypal.com/v1/oauth2/token"
headers = [
  Authorization: "Basic #{ClientID}:#{Secret}" 
]
body = "grant_type=authorization_code&code=#{code}"

HTTPoison.post!(url, body, headers)

This shows the a status_code: 401 and {\"error\":\"invalid_client\",\"error_description\":\"Client Authentication failed\"}", error.. How can this issue be solved?


Solution

  • HTTP Basic Authentication requires the value to be base-64 encoded. Try doing that:

    Authorization: "Basic " <> Base.encode64("#{ClientID}:#{Secret}")