I'm trying to create a request like that, but with ruby.
curl --request POST \
--url '{BASE_URL}/oauth2/token' \
--header 'Authorization: Basic {BASIC_CLIENT}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials
Im trying like this:
require 'uri'
require 'net/http'
url = URI("https://api.userede.com.br/redelabs/oauth2/token")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = http.post(
url,
{ 'grant_type' => 'client_credentials' }.to_json,
{ 'Authorization' => "Basic #{CLIENTID}", 'Content-Type' => 'application/x-www-form-urlencoded'}
)
But i'm getting the following error:
{\"error_description\":\"OAuth 2.0 Parameter: grant_type\",\"error\":\"invalid_request\",\"error_uri\":\"https://datatracker.ietf.org/doc/html/rfc6749#section-5.2\"}
You're formatting the request as JSON, but the system is expecting application/x-www-form-urlencoded.
Replace this:
{ 'grant_type' => 'client_credentials' }.to_json,
with this:
{ 'grant_type' => 'client_credentials' }.to_query,
to conform to the correct payload type.
That may not be the only error, but it is an obvious one.
Props to @max in his answer for the Rails syntax.