Skip to content Skip to sidebar Skip to footer

Appcelerator Titanium Upload To Google Drive - Rest Api In Javascript - Error 404

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

Solution 1:

I was also facing the same type of issue sometime back and following is how I was able to solve it.

Considering I have :

Step 1: Convert blob image to base64 encoded string.

var base64image = Ti.Utils.base64encode(blobData);

Step 2: Create the request body to upload image to drive.

var reqBody = '--foo_bar_baz\nContent-Type: application/json; charset=UTF-8\n\n{"title": "' + myFileName + '"}\n--foo_bar_baz\nContent-Type: image/jpeg\nContent-Transfer-Encoding: base64\n\n' + base64image + '\n--foo_bar_baz--';

Step 3: Set the necessary headers for the request.

client.setRequestHeader('Authorization', "Bearer " + myAccessToken); 
client.setRequestHeader('Content-Type', 'multipart/related; boundary="foo_bar_baz"' );  

The basic snippet :

functionupLoadFile(myAccessToken) {
    var base64image = Ti.Utils.base64encode(blobData); //convert blob to base64 encoded stringvar myFileName = "MyImage.png";
    var reqBody = '--foo_bar_baz\nContent-Type: application/json; charset=UTF-8\n\n{"title": "' + myFileName + '"}\n--foo_bar_baz\nContent-Type: image/jpeg\nContent-Transfer-Encoding: base64\n\n' + base64image + '\n--foo_bar_baz--';
    var url = "https://www.googleapis.com/upload/drive/v2/files";
    var client = Ti.Network.createHTTPClient({
        onload : function(e) {
            Ti.API.info("Received text: " + this.responseText);
            alert('success');
        },
        onerror : function(e) {
            Ti.API.debug(e.error);
            alert('error');
        },
        timeout : 100000000
    });
    client.open("POST", url);
    client.setRequestHeader('Authorization', "Bearer " + myAccessToken); 
    client.setRequestHeader('Content-Type', 'multipart/related; boundary="foo_bar_baz"' ); 
    client.send(reqBody);
}

P.S : This answer uses Drive API v2, also see Drive docs on Manage Upload.

Hope it is helpful.

Post a Comment for "Appcelerator Titanium Upload To Google Drive - Rest Api In Javascript - Error 404"