I am trying to use reddit api in my react application. But when I try to get access token I get the following error.
const response = await axios({
method: "post",
url: "https://www.reddit.com/api/v1/access_token",
auth: {
user: process.env.REACT_APP_REDDIT_CLIENT_ID,
password: process.env.REACT_APP_REDDIT_CLIENT_SECRET,
},
data: {
grant_type: "authorization_code",
code: code,
redirect_uri: "http://localhost:3000/login",
},
});
I am new to Reddit Api and Oauth so I can't really understand what's the issue.
I found the solution. The issue was that parameters sent cannot be in json format. SO I had to change them to url encoded.
const params = new URLSearchParams();
params.append("grant_type", "refresh_token");
params.append("refresh_token", refreshToken);
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
auth: {
username: process.env.REACT_APP_REDDIT_CLIENT_ID,
password: process.env.REACT_APP_REDDIT_CLIENT_SECRET,
},
};
const response = await axios.post(getPrefix(url), params, config);
Also due to my local environment being http. It was causing cors issue. So I prefixed my url with cors-anywhere and it works. I tried using reddit.local
as mentioned but it doesn't seem to work.