【问题标题】:Appcelerator Titanium: Facebook Image Upload failAppcelerator Titanium:Facebook 图片上传失败
【发布时间】:2015-01-25 20:47:29
【问题描述】:

我在我的 Titanium 软件中从 Facebook 上传图片时出错,每次我想从我的应用上传图片时都会收到以下信息:

失败:版本 v2.1 及更高版本已弃用 REST API

但如果我在 KitchenSink 示例应用程序中尝试相同的代码,它会完美运行:

var xhr = Titanium.Network.createHTTPClient({
        onload: function() {
     // first, grab a "handle" to the file where you'll store the downloaded data
            var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'mygraphic.png');
            f.write(this.responseData); // write to the file
            var blob = f.read();
            var data = {
                caption: 'behold, a flower',
                picture: blob
            };
            facebook.request('photos.upload', data, showRequestResult);
        },
        timeout: 10000
    });
    xhr.open('GET','http://www.pur-milch.de/files/www/motive/pm_motiv_kaese.jpg');
    xhr.send(); 

在我的应用中:

function showRequestResult(e) {
    var s = '';
    if (e.success) {
        s = "SUCCESS";
        if (e.result) {
            s += "; " + e.result;
        }
    } else {
        s = "FAIL";
        if (e.error) {
            s += "; " + e.error;
        }
    }
    alert(s);
}
Ti.App.hs_stats.addEventListener('touchend', function(e){
Ti.App.hs_stats.top = 255;
var xhr = Titanium.Network.createHTTPClient({
        onload: function() {
     // first, grab a "handle" to the file where you'll store the downloaded data
            var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'mygraphic.png');
            f.write(this.responseData); // write to the file
            var blob = f.read();
            var data = {
                caption: 'behold, a flower',
                picture: blob
            };
            Ti.App.fb.request('photos.upload', data, showRequestResult);
        },
        timeout: 10000
    });
    xhr.open('GET','http://www.pur-milch.de/files/www/motive/pm_motiv_kaese.jpg');
    xhr.send();     
});

【问题讨论】:

    标签: android facebook facebook-graph-api titanium


    【解决方案1】:

    看起来您正在为 Appcelerator 使用“旧”Facebook 模块?我有用于个人资料和页面的图像上传(虽然页面有点不同,我稍后会解释)。这是一些快速代码(我假设您已经通过 Facebook 进行了身份验证):

     var fb = require('facebook');
     fb.appid = "xxxxxxxxxxxxxxxxx";
     var acc = fb.getAccessToken();
    
     fb.requestWithGraphPath('me/photos?access_token='+ acc, {picture:image, message: data}, "POST", showRequestResult);
    

    图像变量只是一个 blob - 它直接来自来自画廊选择或相机意图的 event.media。 data 是您状态更新的文本。

    在您的 tiapp.xml 中添加以下行:

     <property name="ti.facebook.appid">xxxxxxxxxxxxxxxxx</property>
    

    并且(如果您使用的是 Android 和 iOS - 添加两者或仅添加您正在使用的平台)

     <modules>
        <module platform="android">facebook</module>
        <module platform="iphone">facebook</module>
    </modules>
    

    现在页面有点奇怪:

    var endPoint = 'https://graph.facebook.com/v2.1/' + pid + '/photos?access_token='+ acc;
                                                xhr.open('POST',endPoint);
                                                xhr.send({
                                                    message: data,
                                                    picture: image
                                                });
    

    您必须使用 HTTP 请求,因为无论我尝试什么,我都无法让 requestWithGraphPath() 处理页面。

    pid 是您的页面 ID,您可以获取它,或者您是管理员的页面列表(同样,创建一个新的 HTTP 请求 (xhr) 并使用它):

    xhr.open("GET","https://graph.facebook.com/v2.1/me?fields=accounts{access_token,global_brand_page_name,id,picture}&access_token=" +fb.getAccessToken());
    

    这将返回每个页面的访问令牌、全球品牌名称(基本上是页面名称的干净版本)、它的 ID 和个人资料图片。此 URL 中的访问令牌是您的个人访问令牌(&access_token= 部分)。

    据我所知,这些访问令牌不会针对页面过期,因此您可以将其保存在应用程序中的某个位置,或者如果您真的想安全起见,您可以在每个帖子之前获取一个令牌,但这是有点多。

    奖金:

    如果您想在页面上发布视频:

    var xhr = Titanium.Network.createHTTPClient();
        var endPoint = 'https://graph-video.facebook.com/'+ pid +'/videos?access_token='+ acc;
    xhr.open('POST',endPoint);
    
    xhr.setRequestHeader("enctype", "multipart/form-data");
    
    xhr.send({source:video, description:data});
    

    对于个人资料:

             var acc = fb.getAccessToken();
        var xhr = Titanium.Network.createHTTPClient();
        var endPoint = 'https://graph-video.facebook.com/me/videos?access_token='+ acc;
    xhr.open('POST',endPoint);
    
    xhr.setRequestHeader("enctype", "multipart/form-data");
    
    xhr.send({source:video, description:data});
    

    video 是来自您的相机或图库 event.media 意图的另一个 blob,而 data 是您要用于状态更新的文本。

    【讨论】:

    • 太棒了!在您发布答案之前几分钟,我就失败了。但你的答案比我现在想要的要多。但我想如果我有一些像你描述的那样的行为,我会再次检查你的答案。谢谢!
    • 告诉我进展如何 - 如果您需要查看其他内容,请告诉我!
    • 你能告诉我如何通过 url 将图像上传到 facebook 吗?我的代码不起作用,它只是发布文本。
    • var endPoint = 'https://graph.facebook.com/v2.1/me/photos?access_token='+ acc; xhr.open('POST',endPoint); xhr.send({ message: data, picture: image });
    • 没问题。它奏效了吗?图像变量也可能是错误的类型......就像我说的,确保它是一个 blob。
    猜你喜欢
    • 2014-10-25
    • 2016-04-11
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多