Create folder in google drive using NodeJS
Posted By : Chandan Kumar Singh | 09-Jun-2016
You need drive client id and client secret to make request
- Go to https://console.developers.google.com
- Create an app and get client id and client secret
- Enter redirect url (http://localhost:3000/api/v1/drive)
Now install google node api by command
npm install googleapis --save
Create a file called googleDrive.js and write following piece of code
var google = require('googleapis');
var fs = require('fs');
var OAuth2 = google.auth.OAuth2;
var CLIENT_ID = '7685xxxxxx-sxxxxxxxxxxxxxxxxxxxxxxxxxxxxxm.apps.googleusercontent.com';
var CLIENT_SECRET = 'ixxxxxxxxxxxxxxx4Z';
var REDIRECT_URL = 'http://localhost:3000/api/v1/drive';
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var drive = google.drive({ version: 'v3', auth: oauth2Client });
// generate a url that asks permissions for Google+ and Google Calendar scopes
var scopes = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file'
];
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
});
console.log(url); //this is the url which will authenticate user and redirect to your local server. copy this and paste into browser
module.exports.googleDrive = (function () {
var getTokens = function(code) {
oauth2Client.getToken(code, function(err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if(!err) {
console.log("tokens: ",tokens);
//refresh_token
oauth2Client.setCredentials(tokens);
async.auto({
userFolder : function(next,result) {
return createFolder('test',null,next);
},
cameraFolder:['userFolder',function(next,result){
return createFolder('cam',result.userFolder,next);
}],
videoFolder:['cameraFolder',function(next,result){
return createFolder('video',result.cameraFolder,next);
}]
},function(err,result){
if(!err){
console.log("result: ",result);
}else{
console.log("error: ",err);
}
});
}else {
console.log("error: ",err);
}
});
};
//creating the folder in drive
function createFolder(name,folderId,next) {
var folderIds=[];
if(folderId !== null){
folderIds.push(folderId);
}
var fileMetadata = {
'name' : name,
'mimeType' : 'application/vnd.google-apps.folder',
parents: folderIds
};
drive.files.create({
resource: fileMetadata,
fields: 'id'
}, function(err, file) {
if(err) {
console.log("error creating folder: ",err);
next(err);
} else {
console.log('Folder Id: ', file.id);
next(err,file.id);
}
});
}
return {
getTokens:getTokens
};
})();
Now we have created test folder,inside test cam and inside cam video folder. In next blog I will explaing how to upload video and share it.
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Kumar Singh
Chandan is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate. He loves technologies like Blockchain and IoT.