I\'m a web developer and i\'d like to access to the Google APIs from my business software, I\'ve created a Drive account and I\'d to create a folders and files via code (VB.
Was not aware of the given solution. I did differently by adding a file permission, after having added the file (NodeJS code. insertFile is called after a classic fileUpload by the user) :
insertPermission = function(fileId, value, type, role,authClient,cb) {
var body = {
'value': value,
'type': type,
'role': role
};
var request = drive.permissions.insert({
'fileId': fileId,
'resource': body,
'auth': authClient
}, function (err,response) {
cb(err,response);
});
}
exports.insertFile = function(file,customer,callback) {
exports.authorize(function (err,authClient) {
drive.files.insert({
resource: {
title: file.name,
mimeType: file.mimeType,
parents: [{id:customer.googleDriveFolder}]
},
media: {
mimeType: file.mimeType,
body: file.buffer
},
auth: authClient
}, function(err, response) {
debug('error:', err, 'inserted:', response.id);
if (!err) {
insertPermission(response.id,customer.email,"user","writer",authClient,function (err,resp){
callback(null, resp);
});
}
});
});
};
Note that i'm using JWT with private key kind of authentication, with a service account :
exports.authorize = function(callback) {
if (_tokens && ((_tokens.expiry_date - (new Date()).getTime())>100 )) callback(null, _authClient);
else {
var scope = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.appdata', 'https://www.googleapis.com/auth/drive.apps.readonly',
'https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.scripts'];
var authClient = new google.auth.JWT(
'',
'.pem',
''); //seems we could use an user to impersonate requests, i need to test that..
authClient.authorize(function(err, tokens) {
if (err) {
callback(err);
return;
}
callback(null,authClient);
});
}
};