OAuth2 authentication - "unsupported_grant_type" error at /access_token/ method

129 views
Skip to first unread message

Fandroide

unread,
Dec 8, 2022, 7:44:01 AM12/8/22
to Freesound API
Hi,

I'm not sure if anyone using Freesound API has experienced this issue when trying to get an access token using a web client:

- Error: {"error": "unsupported_grant_type"}

- Code:

var xhr = new XMLHttpRequest();
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(JSON.stringify({
   client_id: 'CLIENT_ID',
   client_secret: 'CLIENT_SECRET',
   code: 'AUTH_CODE ',
   grant_type: 'authorization_code'
}));

I tried with 'Content-Type': 'application/json' but still same result.

PS. In POSTMAN seems to be working fine for example.

Thanks in advance!

Fandroide

unread,
Dec 8, 2022, 7:51:29 AM12/8/22
to Freesound API
If you are interested, the repository containin the sample code is located at: https://github.com/fan-droide/freesound

Thanks again!

Fandroide

unread,
Dec 9, 2022, 5:48:10 AM12/9/22
to Freesound API
Hi again,

I found the way to make it work by this request:

fetch("https://freesound.org/apiv2/oauth2/access_token/?client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET+"&grant_type=authorization_code&code="+AUTH_CODE+"", {
method: "POST"
}).then(response => response.json()).then(response => console.log(JSON.stringify(response)))

I hope it helps someone else.
Reply all
Reply to author
Forward
0 new messages