-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakeCopy.js
More file actions
124 lines (91 loc) · 3.36 KB
/
makeCopy.js
File metadata and controls
124 lines (91 loc) · 3.36 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const {google} = require("googleapis");
const CLIENT_ID = "64080679766-lqssbjjjgi5001n8p6fdvfo98hh421hr.apps.googleusercontent.com";
const CLIENT_SECRET = "D8RvdBWASaIuqGiUQUR929HX";
const REDIRECT_URI = "https://developer.google.com/oauthplayground";
const REFRESH_TOKEN = "1//04OUcH8HC7z0fCgYIARAAGAQSNwF-L9IrdZlverc--eJdgO6_1YuxyHcSuhY_Wt6Uf_2WQVPcrXqfalxdl3nAKU-f7LSz6Vw-xVc"
const oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
)
const AUTH_CODE = "4/0AY0e-g61vVoIMND6S7TPRADQAposd12XLnQKSbBdHA-wP-3ISj0SDo1-N_aoewgMjMflBg";
const code = "4%2F0AY0e-g4GxKqiq3rOCUynrv38_FVjJvR5qgAWGTkYcRKlzg-yWdAg3w03m5yjiEYE0fMJVg&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=64080679766-lqssbjjjgi5001n8p6fdvfo98hh421hr.apps.googleusercontent.com&client_secret=D8RvdBWASaIuqGiUQUR929HX&scope=&grant_type=authorization_code"
const fileId2 = "1WETGBScuLPvASi2e4QjKZfHs3lMgKH9ivQKDQBlHQyI"
const fileId1 = "1BS9tM4I3IOEWYvhn0ZqynsC98oxy2KXixy2SLIrP3Ag"
const fileID = "1ceBIVyWKmLRKtkFbcR1YQnrUVDXVPy0h9hn3p-ZMVas"
oauth2Client.setCredentials({refresh_token : REFRESH_TOKEN});
oauth2Client.refreshAccessToken( function(err , tokens){
if(err){
console.log("can not refresh the acces token")
console.log(err)
}else(console.log(tokens.access_token))
})
async function refreshTimer(){
await setTimeout(()=>{
oauth2Client.refreshAccessToken(function(err , token){});
console.log("access_token refrshed");
refreshTimer()
} , 3400*1000);
}
refreshTimer()
const drive = google.drive({
version : "v3",
auth : oauth2Client
})
async function makeCopy(id = fileID){
try{
const response = await drive.files.copy({
fileId : id
})
console.log("made the API request")
const url = ("https://docs.google.com/spreadsheets/d/" + response.data.id);
console.log(url);
return url;
}catch(err){
console.log(err);
}
}
async function deleteFile(id){
try{
const res = await drive.files.delete({
fileId : id
})
console.log(res);
console.log("SUCCESSFULLY DELETED THE FILE")
}catch(err){
console.log(err)
}
}
// const perm_user = "rishi.dubey2705@gmail.com"
// const perm_file = "17ByGSzIwWHgxltnaxHsIC57uC6NMttaeLk1Jfyso4Ag"
async function grantPermission(user , file){
let userId = ""
// try{
// const res = await drive.permissions.getIdFromEmail({
// email : user
// })
// userId = res.id;
// }catch(err){
// console.log("couldn't fetch the permissions id");
// console.log(err)
// }
try{
console.log(drive.permissions.create)
const res = await drive.permissions.create({
fileId : file,
resource :{
role : "writer",
type : "user",
value : user,
emailAddress: user,
},
})
console.log("SUCCESS!!")
}catch(err){
console.log("couldn't set the permissions for " + user);
console.log(err)
}
}
// grantPermission(perm_user , perm_file)
module.exports = { makeCopy , deleteFile , grantPermission}
//https://docs.google.com/spreadsheets/d/16W9jfH2j1ZaInfNcYlk85zn30jrtG_JGz3SBvmOc0nc/edit?usp=sharing