Created
August 30, 2020 14:02
-
-
Save qnxdev/06a8ec9d7951f1d197ded611073b5568 to your computer and use it in GitHub Desktop.
LinkedIn OAuth callback api function for NextJS, ReactJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default async (req, res) => { | |
const LINKEDIN_URL = "https://www.linkedin.com/oauth/v2/accessToken&grant_type=authorization_code&code=" + req.query.code + "redirect_uri=http://localhost:3000/api/callback&client_id=your_id is at https://www.linkedin.com/developers/apps/&client_secret=your_secret is at https://www.linkedin.com/developers/apps/"; | |
let tok; | |
let resp = await fetch(LINKEDIN_URL, { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
}); | |
if (resp.ok) tok = await resp.json(); | |
let { access_token, expires_in } = tok; | |
let auth = 'Bearer ' + access_token; | |
let u = {}; | |
let user = await fetch("https://api.linkedin.com/v2/me", { | |
method: 'GET', | |
headers: { 'Connection': 'Keep-Alive', 'Authorization': auth }, | |
}); | |
if (user.ok) u = await user.json(); | |
let em = {}; | |
let email = await fetch("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))", { | |
method: 'GET', | |
headers: { 'Connection': 'Keep-Alive', 'Authorization': auth }, | |
}); | |
if (email.ok) em = await email.json(); | |
u = { | |
name: u.localizedFirstName + u.localizedLastName, | |
email: em.elements[0]['handle~']['emailAddress'], | |
password: access_token, | |
expires_in: expires_in | |
}; | |
res.status(200).json(u); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment