-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
57 lines (38 loc) · 1.29 KB
/
app.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
var express = require("express");
var google = require("googleapis");
var app = express();
app.use(express.static('static'));
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2("532417609984-6grimhtlrblhkrjgkvmi1okem6pkfhs2.apps.googleusercontent.com", "JrSHP3fMBl-9CuZkGZmrgB2f", "http://localhost:3000/oauthcallback");
// generate a url that asks permissions for Google+ and Google Calendar scopes
var scopes = [
'https://www.googleapis.com/auth/gmail.modify'
];
var url = oauth2Client.generateAuthUrl({
access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)
scope: scopes // If you only need one scope you can pass it as string
});
app.get("/url", function(req, res) {
res.send(url);
});
app.get("/tokens", function(req, res) {
var code = req.query.code;
console.log(code);
oauth2Client.getToken(code, function(err, tokens) {
if (err) {
console.log(err);
res.send(err);
return;
}
console.log("allright!!!!");
console.log(err);
console.log(tokens);
oauth2Client.setCredentials(tokens);
res.send(tokens);
});
});