Using HTTPoison to get access token

Hello All,
I am new to Elixir and so far having fun learning. I am trying to call a REST API using HTTPoison and am running into an Issue.

Here is what I am trying to do.

I need to generate an access token by sending a POST request to the authorization server:
https://test.example.com/v2/security/oauth2/token

This is the curl command to get the access token.

curl “https://test.example.com/v2/security/oauth2/token
-H “Content-Type: application/x-www-form-urlencoded”
-d “grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}”

I want to use HTTPoison to get the access token which will then be used to pull the data.

Anyone has any example how to convert the curl to HTTPoison?

Regards,
Venkat

As a starting point, try this online converter from cURL to several different languages. Elixir is included.

https://curl.trillworks.com/#elixir

4 Likes

Here’s how I get the token:

HTTPoison.post!(
  "https://test.example.com/v2/security/oauth2/token",
  {:form,
    [
      grant_type: "client_credentials",
      client_id: @client_id,
      client_secret: @client_secret
    ]}
)
1 Like

Thank you @nidu and @dimitarvp. I was able to use the HTTPoison.post!.