I've been trying to get a login working using the Resource Owner Password Grant oAuth, however I can't seem to make it work.
I didn't build the backend I'm sending the request to, I'm using Invision Power Board's. I cannot find any example requests using a simple fetch
so I'm just not sure if I'm doing anything wrong.
Here's my request being sent client-side:
const form = {
'grant_type': 'password',
'username': 'username',
'password': 'pass',
'scope': 'profile',
'client_id': 'client_id',
}
var formBody = [];
for (var property in form) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(form[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
await fetch(`https://example.com/oauth/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
})
All I get back is an error:
{
"error": "invalid_request",
"error_description": "request must be a POST request"
}
Did I format this the wrong way or am I missing something? I've been following the oAuth docs but maybe I've misunderstood how to do this.
Is it a problem with my server?
Edit - Example request with reqbin:
POST /oauth/token HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 85
grant_type=password&username=username&password=pass&scope=profile&client_id=client_id
Strangely enough the response here is:
{
"error": "invalid_client"
}