Posting this in case it helps anyone:
I've been working on the integration with an OAuth2.0 server provided by one of our partners. The vendor's API documentation asks to provide the "Content-Type: application/x-www-form-urlencoded" header on each request. However, while attempting to send a POST request to get the access token, the server kept responding with an HTTP 400 "Invalid request" error. After several tries, I realized that the vendor's OAUTh server seems to be expecting the value of the "redirect_uri" to be unencoded. Therefore, as a workaround, I had to manually encode each value of the payload and leave the "redirect_uri" unencoded.
Here's an example of the code for your reference:
const getAccessToken = () => {
let payloadEncoded = '';
payloadEncoded += 'client_id=' + <OAUTH CLIENT ID>
payloadEncoded += '&client_secret=' + <OAUTH SECRET>
payloadEncoded += '&grant_type=authorization_code';
payloadEncoded += '&code=' + <OAUTH CODE>
payloadEncoded += '&redirect_uri=https://<REDIRECT URI UNENCODED>';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
body: payloadEncoded
};
fetch('https://<OAUTH SERVER HOST>/c2id/token', options)
.then(response => response.json())
.then(data => {console.log(data);
.catch(error => console.error(error));
}
Obviously, I will reach out to the vendor to make the correction on their side.
I hope this helps!