generated from nylas-samples/nylas-code-samples-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
88 lines (74 loc) · 2.34 KB
/
index.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
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
import "dotenv/config";
import express from "express";
import Nylas from "nylas";
const config = {
clientId: process.env.NYLAS_CLIENT_ID,
callbackUri: "http://localhost:3000/oauth/exchange",
apiKey: process.env.NYLAS_API_KEY,
apiUri: process.env.NYLAS_API_URI,
};
const nylas = new Nylas({
apiKey: config.apiKey,
apiUri: config.apiUri, // "https://api.us.nylas.com" or "https://api.eu.nylas.com"
});
const app = express();
const port = 3000;
// start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
// route to initialize authentication
app.get("/nylas/auth", (req, res) => {
const authUrl = nylas.auth.urlForOAuth2({
clientId: config.clientId,
redirectUri: config.callbackUri,
});
res.redirect(authUrl);
});
// auth callback route
app.get("/oauth/exchange", async (req, res) => {
console.log("Received callback from Nylas");
const code = req.query.code;
if (!code) {
res.status(400).send("No authorization code returned from Nylas");
return;
}
const codeExchangePayload = {
clientSecret: config.apiKey,
clientId: config.clientId,
redirectUri: config.callbackUri,
code,
};
try {
const response = await nylas.auth.exchangeCodeForToken(codeExchangePayload);
const { grantId } = response;
// NB: This stores in RAM
// In a real app you would store this in a database, associated with a user
process.env.USER_GRANT_ID = grantId;
res.json({ message: "OAuth2 flow completed successfully for grant ID: " + grantId });
} catch (error) {
res.status(500).send("Failed to exchange authorization code for token");
}
});
// route to create contact
app.get("/nylas/create-contact", async (req, res) => {
try {
const contact = await nylas.contacts.create({
identifier: process.env.USER_GRANT_ID,
requestBody: {
givenName: "My",
middleName: "Nylas",
surname: "Friend",
notes: "Make sure to keep in touch!",
emails: [{type: 'work', email: '[email protected]'}],
phoneNumbers: [{type: 'work', number: '(555) 555-5555'}],
webPages: [{type: 'other', url: 'nylas.com'}]
}
})
console.log('Contact:', JSON.stringify(contact))
res.json(contact);
} catch (error) {
console.error('Error to create contact:', error)
res.status(500).send("Failed to create contact");
}
});