Im trying to upload an image to google drive using appcelerator titanium. I have got OAuth and i have an access_token which is valid.
Unfortunately when i try and upload the image i get a 404 http error.
function Upload(blob){
Titanium.API.info(googleAuth.getAccessToken());
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var request = Ti.Network.createHTTPClient({
onload : function(e) {
alert(this.requestText); },
onerror : function(e) {
Titanium.UI.createAlertDialog({
title : 'Error',
message : 'unable to upload' + JSON.stringify(e)
}).show();
},
timeout : 60000
});
var metadata = {
'title' : "image1.png",
'mimeType': 'application/json',
};
var mediaRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
blob +
close_delim;
var uploadRequest = {
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"',
'access_token' : googleAuth.getAccessToken()
},
'body': mediaRequestBody};
var url = 'https://www.googleapis.com/upload/drivev2/files?uploadType=multipart';
request.open("POST", url);
request.send(uploadRequest);
}
The 'Blob' i pass through is retrieved by doing:
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
f.write(image);
var url = f.nativePath;
var blob = f.read();
I think close but it's probably my http request.
Thanks.!
I was also facing the same type of issue sometime back and following is how I was able to solve it.
Considering I have :
blob
of the image which I have to upload.Step 1: Convert blob image to base64 encoded string.
Step 2: Create the request body to upload image to drive.
Step 3: Set the necessary headers for the request.
The basic snippet :
P.S : This answer uses Drive API v2, also see Drive docs on Manage Upload.
Hope it is helpful.