I am having trouble with the POST request format for retrieving the access token with the Reddit API. I am following the instructions for OAuth2 and was able to parse the URL for the initial 'code' after the user grants permission but I don't know what to include in the post request exactly. This is what I have so far, where returnCode is the code parsed from the URL, but I get a 401 response.
async function fetchToken(returnCode) {
const form = new FormData();
form.set('grant_type', 'authorization_code');
form.set('code', returnCode);
form.set('redirect_uri', 'http://localhost:3000/')
const response = await fetch('https://www.reddit.com/api/v1/access_token', {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
authorization: `Basic clientID + clientSecret`
},
body: form
}).then(r => r.json())
console.log(response);
}
Any help would be appreciated.