-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (64 loc) · 2.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var express = require("express");
var google = require("googleapis");
var Clentid = "your client ID";
var Client_screre = "your clent Secret";
var redirecturl = "your url where you want to refirect ex- http://localhost:3000/oauthcallback";
var app = express();
app.use(express.static('static'));
// to set the port 3000
app.listen(process.env.PORT || 3000,function(err)
{
if(err)
{
console.log(err);
console.log("listen failed");
}
else{ console.log("listen on 3000 port");}
});
//create OAuth2 object to set credentials in further
var OAuth2 = google.auth.OAuth2; // this is Oauth2 object
// set all credentials
var oauth2Client = new OAuth2(Clentid, Client_screre, redirecturl);
// generate a url that asks permissions for drive scopes
var scopes = [
'https://www.googleapis.com/auth/drive.metadata.readonly'
];
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
app.get("/url", function(req, res) {
res.send(url);
});
app.get("/tokens", function(req, res) {
var code = req.query.code;
console.log(code);
// set code in oauth2Client and get token from server to further process
oauth2Client.getToken(code, function(err, tokens) {
if (err) {
console.log(err);
res.send(err);
return;
}
// after get token set credentials which we get from above
oauth2Client.setCredentials({
access_token: tokens.access_token,
refresh_token: tokens.refresh_tokens
});
// print all file
var service = google.drive('v3');
service.files.list({auth: oauth2Client,}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var files = response.files;
if (files.length == 0) {
console.log('No files found.');
} else {
return res.send(files);
}
});
//code end here
});
});