Guys I have an error that in my NodeJS rest API, and can't solve this. My idea is to make a github login, this app works like this:
Redirection to github returning temporary code in callback.
Send this temporary code to my REST API and make a fetch request to another GitHub API endpoint. This request must return
access_token = 12345
(this access token is an example), to send this token to the frontend, convert it to a JWT token and then store it in a localStorage for use.
My code in NodeJS
router.post("/users/github/:code",function(req,res){
fetch('https://github.com/login/oauth/access_token/', {
method: 'GET',
client_id: 'xxxx',
client_secret: 'xxxx',
code: req.params.code,
accept: 'json',
})
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
});
PS: I use node-fetch
module for this. https://www.npmjs.com/package/node-fetch
Your request is wrong, the correct thing is like this:
Look at the headers and body part . In a POST request , you must send the data in the body (
body
) of the request. If you send a JSON, you must parse it to a string (usingJSON.stringify
) and, in addition, you must specify the nameContent-Type
of the information you are sending, in your caseapplication/json
.