-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathlinkedin.js
62 lines (52 loc) · 1.64 KB
/
linkedin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use strict";
const createApplication = require(".");
const { AuthorizationCode } = require("..");
createApplication(({ app, callbackUrl }) => {
const secret = process.env.CLIENT_SECRET;
const client = new AuthorizationCode({
client: {
id: process.env.CLIENT_ID,
secret,
},
options: {
authorizationMethod: 'body',
},
auth: {
authorizeHost: "https://www.linkedin.com",
authorizePath: "/oauth/v2/authorization",
tokenHost: "https://www.linkedin.com",
tokenPath: "/oauth/v2/accessToken",
},
});
// Authorization uri definition
const authorizationUri = client.authorizeURL({
redirect_uri: callbackUrl,
scope: ["r_liteprofile", "r_emailaddress"],
state: "random123",
});
// Initial page redirecting to Github
app.get("/oauth/linkedin", (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get("/oauth/linkedin/callback", async (req, res) => {
const { code } = req.query;
const options = {
code,
redirect_uri: callbackUrl,
scope: "r_liteprofile, r_emailaddress"
};
try {
const accessToken = await client.getToken(options);
console.log("The resulting token: ", accessToken.token);
return res.status(200).json(accessToken.token);
} catch (error) {
console.error("Access Token Error", error.message);
return res.status(500).json("Authentication failed");
}
});
app.get("/", (req, res) => {
res.send('Hello<br><a href="/oauth/linkedin">Log in with LinkedIn</a>');
});
});