Appcelerator Titanium Loading on Google Drive - Rest API in Javascript - Error 404

I am trying to upload an image to google drive using the titanium appcelerator. I have OAuth and I have an access_token that is valid.

Unfortunately, when I try to upload an 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);

      

}

After going through 'Blob' I get:

var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
f.write(image);
var url = f.nativePath;
var blob = f.read();

      

I think that's close, but this is probably my http request.

Thank.

This is the error I got jsonified

+3


source to share


1 answer


I also ran into the same type of problem, sometime ago, and in the following way how I was able to solve it.

Given that I have:

Step 1: Convert the blob image to base64 encoding.

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

      

Step 2: Create a request body to upload the image to disk.

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 required headers for the request.

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

      

Main snippet:

function upLoadFile(myAccessToken) {
    var base64image = Ti.Utils.base64encode(blobData); //convert blob to base64 encoded string

    var 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);
}

      

PS: This answer uses API Driver v2 , also see the Drive docs on Boot Management .

Hope this is helpful.

+1


source







All Articles