I've been trying to create this very simple web app that is using google drive appDataFolder to store and read its state, in json format. However, the documentation for v3 drive api doesn't contain comprehensive examples on how to achieve this easily.
So far I'm able to authorize and load the google drive api, but I wasn't able to create the configuration json file if it doesn't exist, or update its contents or read if it exists.
The calls I'm making are to: gapi.client.drive.files.get to retrieve the config json, gapi.client.drive.files.create to create the config json and gapi.client.drive.files.update to update its contents.
I've been trying to use the javascript Blob object to represent my file and its contents, but nothing has worked.
Given I have the config like for example { test: true } stringified and the file name my-app.json, how would I call the gapi.client.drive.files API to be able to create/update/read this config?
The retrieve part I tried to do it by first getting a list of the files in the app folder, match the config file by name, get its id, then request again for that file using the id. But since I wasn't able to create the file, I'm not sure if it works.
Code currently looks like this (important to note that this code gets build and it's run in the client browser, not on the server):
var config = require('../config/google-drive-config');
var authorize = function (immediate) {
return gapi.auth.authorize({
'client_id': config.clientId,
scope: config.scopes.join(' '),
immediate: !!immediate
});
};
var loadDriveAPI = function () {
return gapi.client.load(
config.apiName,
config.apiVersion
);
};
var loadAppDataFileId = function () {
return gapi.client.drive.files
.list({
spaces: 'appDataFolder'
})
.then(function(response) {
return _(response.files)
.find({ name: config.appDataFile })
.get('id')
.value();
});
};
var loadAppData = function (fileId) {
return gapi.client.drive.files
.get({
'fileId': fileId
});
};
var saveAppData = function (appData, fileId) {
var resource = {
'name': config.appDataFile,
'parents': 'appDataFolder'
};
var media = {
mimeType: 'application/json',
body: new Blob([JSON.stringify(appData)], { type: 'application/json' })
};
if (fileId) {
return gapi.client.drive.files
.update({
fileId: fileId,
media: media
});
}
return gapi.client.drive.files
.create({
resource: resource,
media: media,
fields: 'id'
});
};
module.exports = {
authorize: authorize,
loadDriveAPI: loadDriveAPI,
loadAppDataFileId: loadAppDataFileId,
loadAppData: loadAppData,
saveAppData: saveAppData
};