Curl - Connect to a REST API with an OAuth Bearer Token

by
Jeremy Canfield |
Updated: January 24 2024
| Curl articles
There are a couple similar but unique ways to connect to a REST API without basic authentication (username / password).
- Connect to a REST API with an API Key
- Connect to a REST API with an OAuth Bearer Token (this article)
Let's say there is an OAuth API that is configured to allow connections with a Bearer Token. You would first use the curl command to get your OAuth Bearer Token from the API.
curl
--request POST
--url https://api.example.com/foo/bar/
--data '{ "Username": "john.doe", "Password": "itsasecret" }'
--header "Content-Type: application/json"
Something like this should be returned. In this example, "abc123" is the Bearer Token.
{
"access_token":"abc123",
"refresh_token":"T2w8o3xgET2K9Dh8abqnWQ==",
"expires_in":31536000,
"expires":1655380828,
"token_type":"Bearer",
"refresh_until":1655380828
}
You would then issue a command like this, using the Bearer Token.
curl
--request GET
--header "Authorization: Bearer abc123"
--header "Accept: application/json"
--url "https://api.example.com/foo/bar"
Did you find this article helpful?
If so, consider buying me a coffee over at