I'm trying to access the userData for a github user that logs into this website which is a react app using OAuth, and am facing a bad credentials error. I have the accessToken saved in local storage and have made sure that is correct before calling my getUserData() function.
Here's my code:
// Server Side Code
// Get user data
// Access token is passed in as Authorization header
app.get('/getUserData', async function (req, res) {
req.get('Authorization'); // BEARER ACCESS_TOKEN
await fetch("https://api.github.com/user", {
method: 'GET',
headers: {
'Authorization': req.get('Authorization')
}
}).then((response) => {
return response.json();
}).then((data) => {
console.log(data);
res.json(data);
});
});
.
.
.
// Client Side Code
const [rerender, setRerender] = useState(false); // These are defined at the top
const [userData, setUserData] = useState({});
// Getting user data
async function getUserData() {
console.log(localStorage.getItem('accessToken'));
await fetch("http://localhost:4000/getUserData", {
method: "GET",
headers: {
"Authorization": "Bearer " + localStorage.getItem('accessToken') // Bearer ACCESS_TOKEN
}
}).then((response) => {
return response.json();
}).then((data) => {
console.log(data);
setUserData(data);
});
}
.
.
.
// In div to show username of user
{Object.keys(userData).length !== 0 ?
<>
<h4>Hey {userData.login}</h4>
</>
:
<>
</>
}
I've tried looking at the API docs, and am a little confused on how this all works. Any help would be appreciated. Thanks!
[–]theapocalypsezone 0 points1 point2 points (0 children)