Appcelerator钛上传到谷歌驱动器 - 其余API在JavaScript - 错误404 [英] Appcelerator Titanium upload to google drive - Rest API in Javascript - Error 404

查看:208
本文介绍了Appcelerator钛上传到谷歌驱动器 - 其余API在JavaScript - 错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用appcelerator titanium将图像上传到谷歌驱动器。
我有OAuth,并且有一个有效的access_token。

不幸的是,当我尝试上传图片时,我得到一个404 http错误。

 函数上传(blob){
Titanium.API.info(googleAuth.getAccessToken());
const boundary ='------- 314159265358979323846';
const delimiter =\r\\\
--+ boundary +\r\\\
;
const close_delim =\r\\\
--+ boundary + - ;
$ b var request = Ti.Network.createHTTPClient({

onload:function(e){

alert(this.requestText);},

onerror:function(e){
Titanium.UI.createAlertDialog({
title:'Error',
message:'无法上传'+ JSON。 stringify(e)
})。show();
},
timeout:60000

});

var metadata = {
'title':image1.png,
'mimeType':'application / json',
};

var mediaRequestBody =
分隔符+
'Content-Type:application / json\r\\\
\r\\\
'+
JSON.stringify (元数据)+
分隔符+
blob +
close_delim;

var uploadRequest = {
'headers':{
'Content-Type':'multipart / mixed; border =''+ border +'',
'access_token':googleAuth.getAccessToken()
},
'body':mediaRequestBody};



var url ='https://www.googleapis.com/upload/drivev2/files?uploadType=multipart';
request.open(POST,url);
request.send(uploadRequest);

}



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

我觉得很近,但可能是我的http请求。

谢谢。!



这是我得到json化的错误

解决方案



考虑到我有以下情况:





第1步 g>将blob图像转换为base64编码的字符串。

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

第2步:创建要将图片上传到驱动器的请求正文

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

第3步:为请求设置必要的标头。

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

基本片段:

  function upLoadFile(myAccessToken){
var base64image = Ti.Utils.base64encode(blobData); //将blob转换为base64编码的字符串

var myFileName =MyImage.png;
var reqBody ='--foo_bar_baz\\\
Content-Type:application / json; charset = UTF-8 \\\
\\\
{title:'+ myFileName +'} \\\
- foo_bar_baz\\\
Content-Type:image / jpeg\\\
Content-Transfer-Encoding:base64\ n \ n'+ base64image +'\\\
- 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);





这个答案使用 Drive API v2 ,另请参阅在管理上传



希望它有帮助。

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.!

This is the error i get jsonified

解决方案

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 :

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

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

Hope it is helpful.

这篇关于Appcelerator钛上传到谷歌驱动器 - 其余API在JavaScript - 错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆