Reputation: 29
I've had a working application but today it has decided to break so I assume Google has made some sort of changes. CSV data is passed to my function which then uploads the file and converts it to google sheet format (well this is what it did do)
I'm working with JS and there are no examples of in the docs on how to do it with V3. I have read that instead of running 'conver':true in the request paramters in the client you just specify your desired mimeType and the API will do that for you.
here is where it states that in the docs ->
https://developers.google.com/drive/v3/web/migration#other_changes
Imports to Google Docs formats are now requested by setting the appropriate target mimeType in the resource body, rather than specifying ?convert=true.
here is the code I am looking at now. It is returning a 400 error
whenever I leave the mimeType as 'application/vnd.google-apps.spreadsheet'
gapiService.uploadCSVToGoogleSheet =function(data, sheetTitle) {
// create file
cnsl("in gapiService.uploadCSVToGoogleSheet()!!!", "start");
gapi.client.load('drive', 'v3',
upload
);
function upload (){
var fileData = new Blob(data, {type: "text/csv-creation", fileName: "testName"});
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function (e) {
console.log("loaded reader ");
console.log(contentType);
var metadata = {
'name': sheetTitle + "-" + getCurrentDate(),
'mimeType': 'application/vnd.google-apps.spreadsheet'
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
// POST INFO
var request = gapi.client.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': {
'uploadType': 'multipart'
// 'convert': true
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
request.execute(gapiService.afterFileInsert);
} //endof reader onload
}
function getCurrentDate() {
console.log("in get GetCurrentDATE");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + '_' + dd + '_' + yyyy;
return today;
};
// console.log(fileData, null, 0);
};
`
Upvotes: 1
Views: 674
Reputation: 7771
You received error 400 if a required field or parameter has not been provided, the value supplied is invalid, or the combination of provided fields is invalid.
This error also can be thrown when trying to add a duplicate parent to a Drive item. It can also be thrown when trying to add a parent that would create a cycle in the directory graph.
For more information on how to upload Google sheet formatting in Google Drive, check these related SO questions:
Upvotes: 0