简体   繁体   English

Appcelerator Titanium:Facebook图片上传失败

[英]Appcelerator Titanium: Facebook Image Upload fail

i have an error with the Image Upload from Facebook in my Titanium Software, everytime i want to upload an image from my App i get this: 我在Titanium软件中从Facebook上传图像时遇到错误,每次我想从我的应用程序上传图像时,我都会得到以下信息:

Fail: REST API is deprecated for versions v2.1 and higher 失败:不支持REST API v2.1及更高版本

But if i try the same code in the KitchenSink example app, it works perfect: 但是,如果我在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(); 

And in my App: 在我的应用中:

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

Looks like you're using the 'old' Facebook module for Appcelerator? 看起来您正在为Appcelerator使用“旧” Facebook模块? I have image uploads working for Profiles and Pages (although Pages is a bit different, I'll explain later). 我有用于“个人资料”和“页面”的图片上传功能(尽管“页面”有点不同,我将在后面解释)。 Here's some quick code (I assume you already authenticated with 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);

The image variable is just a blob - It comes directly from event.media from a gallery selection or camera intent. image变量只是一个斑点-它直接来自来自库选择或摄像机意图的event.media。 data is the text for your status update. data是状态更新的文本。

In your tiapp.xml add these lines: 在您的tiapp.xml中添加以下行:

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

and (if you're using Android and iOS - add both or just the platform you're using) 和(如果您使用的是Android和iOS,请同时添加或仅添加所使用的平台)

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

Now Pages were a bit strange: 现在Pages有点奇怪:

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

You have to use an HTTP Request, as I couldn't get the requestWithGraphPath() to work with pages no matter what I tried. 您必须使用HTTP请求,因为无论尝试什么,我都无法使用requestWithGraphPath()处理页面。

pid is your page ID and you can get it, or a list of pages you are an admin for like so (again, create a new HTTP Request (xhr) and use this): 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());

This will return the access token for each page, the global brand name (basically a clean version of the page name), it's id and the profile picture. 这将返回每个页面的访问令牌,全局品牌名称(基本上是页面名称的干净版本),ID和个人资料图片。 The access token in this URL is YOUR personal access token (the &access_token= part). 此URL中的访问令牌是您的个人访问令牌(&access_token =部分)。

As far as I can tell, these access tokens don't expire for pages, so you can save it in your app somewhere or if you REALLY want to be safe, you could grab a token before each post, but that's a bit much. 据我所知,这些访问令牌不会在页面中过期,因此您可以将其保存在应用程序中的某个位置,或者如果您确实希望安全,则可以在每次发布之前获取一个令牌,但这有点多。

BONUS: 奖金:

If you want to do video posts to pages: 如果要在页面上发布视频:

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

and for profiles: 对于个人资料:

         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 is another blob from either your camera or gallery event.media intent and data is the text you want to use for the status update. 视频是相机或图库事件中的另一个斑点。媒体意图和数据是您要用于状态更新的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM