我有来自Facebook的图片上传错误在我的钛软件,每次我要上传从我的应用程序的图像,我得到这样的:
失败:REST API是pcated为版本2.1及更高
德$ P $但如果我尝试在同一code在的KitchenSink示例应用程序,它可以完美运行:
VAR XHR = Titanium.Network.createHTTPClient({
的onload:函数(){
//首先,抓住了把柄的文件在这里您可以存储下载的数据
变种F = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'mygraphic.png');
f.write(this.responseData); //写入文件
变种斑点= f.read();
VAR数据= {
标题:看哪,有一种花,
图片:BLOB
};
facebook.request('photos.upload',数据,showRequestResult);
},
超时:10000
});
xhr.open(GET,HTTP://www.pur-milch.de/files/www/motive/pm_motiv_kaese.jpg');
xhr.send();
和我的应用程序:
函数showRequestResult(五){
VAR S ='';
如果(e.success){
S =成功;
如果(e.result){
S + =;+ e.result;
}
} 其他 {
S =不及格;
如果(e.error){
S + =;+ e.error;
}
}
警报(S);
}
Ti.App.hs_stats.addEventListener('touchend',功能(五){
Ti.App.hs_stats.top = 255;
VAR XHR = Titanium.Network.createHTTPClient({
的onload:函数(){
//首先,抓住了把柄的文件在这里您可以存储下载的数据
变种F = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'mygraphic.png');
f.write(this.responseData); //写入文件
变种斑点= f.read();
VAR数据= {
标题:看哪,有一种花,
图片:BLOB
};
Ti.App.fb.request('photos.upload',数据,showRequestResult);
},
超时:10000
});
xhr.open(GET,HTTP://www.pur-milch.de/files/www/motive/pm_motiv_kaese.jpg');
xhr.send();
});
解决方案
看起来像您使用的是'老'的Facebook模块Appcelerator的?我有图片上传工作档案和页面(虽然网页是一个有点不同,我会在后面解释)。下面是一些快速code(我假设你已经与Facebook的身份验证):
变种FB =要求(的Facebook');
fb.appid =xxxxxxxxxxxxxxxxx;
VAR ACC = fb.getAccessToken();
fb.requestWithGraphPath('?我/照片access_token ='+ ACC,{图片:图片,留言:数据},POST,showRequestResult);
图片变量只是一个blob - 它是直接从画廊的选择或相机意图event.media。数据是文本你的状态更新。
在您的tiapp.xml加上这些行:
<属性名=ti.facebook.appid> xxxxxxxxxxxxxxxxx< /性>
和(如果你使用的是Android和iOS的 - 同时添加或只是你使用的平台)
<模块>
<模块平台=机器人> Facebook和LT; /模块>
<模块平台=iphone> Facebook和LT; /模块>
< /模块>
现在的页面是一个有点奇怪:
VAR终点='https://graph.facebook.com/v2.1/'+ PID +/照片access_token =?'+ ACC;
xhr.open(POST,终点);
xhr.send({
消息:数据,
图片:图片
});
您必须使用一个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终点='https://graph-video.facebook.com/'+ PID +'/视频access_token ='+ ACC?;
xhr.open(POST,终点);
xhr.setRequestHeader(加密类型,多部分/表单数据);
xhr.send({来源:视频描述:数据});
和型材:
VAR ACC = fb.getAccessToken();
VAR XHR = Titanium.Network.createHTTPClient();
VAR终点='https://graph-video.facebook.com/me/videos?access_token='+ ACC;
xhr.open(POST,终点);
xhr.setRequestHeader(加密类型,多部分/表单数据);
xhr.send({来源:视频描述:数据});
视频无论从相机或画廊event.media意图和数据的另一种斑点是你要使用的状态更新文本。
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:
Fail: REST API is deprecated for versions v2.1 and higher
But if i try the same code in the KitchenSink example app, it works perfect:
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? 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):
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. data is the text for your status update.
In your tiapp.xml add these lines:
<property name="ti.facebook.appid">xxxxxxxxxxxxxxxxx</property>
and (if you're using Android and iOS - add both or just the platform you're using)
<modules>
<module platform="android">facebook</module>
<module platform="iphone">facebook</module>
</modules>
Now Pages were a bit strange:
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.
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):
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. The access token in this URL is YOUR personal access token (the &access_token= part).
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.