-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathoauth-member-auth-redirect.ts
More file actions
91 lines (83 loc) · 2.7 KB
/
oauth-member-auth-redirect.ts
File metadata and controls
91 lines (83 loc) · 2.7 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* This example illustrates a basic example of the oauth authorization code flow.
*
* Pre-requisites:
* 1. Add CLIENT_ID, CLIENT_SECRET, and OAUTH2_REDIRECT_URL variables to the examples/.env file.
* 2. The associated developer app you are using should have access to r_liteprofile, which can be
* obtained through requesting the self-serve Sign In With LinkedIn API product on the LinkedIn
* Developer Portal.
* 3. Set your developer app's OAuth redirect URL to "http://localhost:3000/oauth"
*
* Steps:
* 1. Run script
* 2. Navigate to localhost:3000
* 3. Login as LinkedIn member and authorize application
* 4. View member profile data
*/
import express from 'express';
import dotenv from 'dotenv';
import { AuthClient, RestliClient } from 'linkedin-api-client';
dotenv.config();
const app = express();
const port = 3000;
// Start off with no access token
let accessToken = '';
// Initialize auth and restli clients
if (!(process.env.CLIENT_ID && process.env.CLIENT_SECRET && process.env.OAUTH2_REDIRECT_URL)) {
throw new Error(
'The CLIENT_ID, CLIENT_SECRET, and OAUTH2_REDIRECT_URL variables must be set in the .env file.'
);
}
const authClient = new AuthClient({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUrl: process.env.OAUTH2_REDIRECT_URL
});
const restliClient = new RestliClient();
restliClient.setDebugParams({ enabled: true });
// Route to display profile details
app.get('/', (_req, res) => {
if (!accessToken) {
// If no access token, have member authorize again
res.redirect(authClient.generateMemberAuthorizationUrl(['r_liteprofile']));
} else {
// Fetch profile details
restliClient
.get({
resourcePath: '/me',
accessToken
})
.then((response) => {
res.json(response.data);
})
.catch(() => {
res.send('Error encountered while fetching profile.');
});
}
});
// OAuth callback route handler
app.get('/oauth', (req, res) => {
const authCode = req.query?.code as string;
if (authCode) {
// Exchange auth code for an access token and redirect to main page
authClient
.exchangeAuthCodeForAccessToken(authCode)
.then((response) => {
accessToken = response.access_token;
console.log(`Access token: ${accessToken}`);
res.redirect('/');
})
.catch(() => {
res.send('Error exchanging auth code for access token.');
});
} else {
if (req.query?.error_description) {
res.send(`Error: ${req.query?.error_description as string}`);
} else {
res.send('Expecting "code" query parameter');
}
}
});
app.listen(port, () => {
console.log(`Navigate to example app at http://localhost:${port}`);
});