Facebook PublishPost with image (update)
Soon, facebook api method PublishUserAction (flash api method) gets deprecated (December 20, 2009). It is suggested to use Facebook.streamPublish instead, and you can find this method in facebook flash api as PublishPost method. If you have problems to make it work take a look at the next simple example that is using image attachment and actionLinkData (as prelink anywhere).
Working part of application:
var message:String = "Hallo world";
var attachment:Object = {media: [{
type: "image",
src: "http://www.somedomain.com/someimage.jpg",
href: "http://www.somedomain.com/sometarget.html"
}]};
var actionLinkData:ActionLinkData = new ActionLinkData();
actionLinkData.href = "http://www.someapplication.com/sometarget.html";
actionLinkData.text = "Uber application";
var post:PublishPost = new PublishPost(message, attachment, [actionLinkData], fbook.uid);
var call:FacebookCall = fbook.post(post);
Update (Feb 12, 2009): Type-proper usage:
import com.facebook.commands.stream.PublishPost; import com.facebook.data.stream.ActionLinkData; import com.facebook.data.stream.AttachmentData; import com.facebook.data.stream.MediaTypes; import com.facebook.data.stream.StreamMediaData; var message:String = "Some status message"; // can be empty var media:StreamMediaData = new StreamMediaData(); media.type = MediaTypes.PHOTO; // MediaTypes defines "link", "video", "photo" but // you should use "image", "flash", or "mp3" // http://wiki.developers.facebook.com/index.php/Attachment_(Streams) media.src = "http://www.somedomain.com/someimage.jpg"; media.href = "http://www.somedomain.com/go/somewhere.html"; var attachment:AttachmentData = new AttachmentData(); attachment.media = [media]; attachment.href = "http://www.somedomain.com/go/somewhere.html"; attachment.name = "Image title"; attachment.caption = "Some subtitle"; attachment.description = "Lorem Ipsum text about your image"; var link:ActionLinkData = new ActionLinkData(); link.href = "http://www.somedomain.com/your_application.html"; link.text = "Your application name"; var post:PublishPost = new PublishPost(message, attachment, [link], facebook.uid);
Read more about Stream.publish and Attachment (Streams)
By default, the photo and the Flash object are rendered in a space up to 90 pixels in both height or width, with an emphasis on scaling the image to be 90 pixels wide (90×90 px max).

Thanks, very helpfull post
That’s cool!unfortunately I can’t make it work: when I send the PublishPost everything seems work, but then I debug it and see a error code 200 (The application does not have permission to perform this action.). Any ideas how to fix this? Can you post the complete code?
Hi, error code 200 may be caused by insufficient permissions. Your app needs publish_stream permission, check out this post http://blog.yoz.sk/2009/10/fb-connect-showpermissiondialog/
I have some errors with this line : var post:PublishPost = new PublishPost(message, attachment, [actionLinkData], fbook.uid);
Error 1067 : Contrainte implicite d’une valeur du type Array vers un type sans rapport com.facebook.data.feed:TemplateData.
Error 1067 : Contrainte implicite d’une valeur du type String vers un type sans rapport com.facebook.data.feed:ActionLinkCollection.
Error 1118: Contrainte implicite d’une valeur du type statique Object vers un type peut-être sans rapport String.
I have a Facebook app with an iframe and I would like to publish post on user’s wall.
Please help me !!
Hi Poudib, do you use the latest facebook flex api? which version do you use?
This is great thanks very much. Took me a bit to get it to work, but it fails silently when using ‘media.type = MediaTypes.PHOTO;’ if you use ‘media.type = “image”‘ it works fine.
Hi, great example.
do you know if it is posible to get the extented persmisions when calling from a remote site, and not at site embeded in the facebook page?
Hi Tommy,
extended permissions comes with facebook application instance, no matter whether it is web or desktop application type. Once your user grants permissions to your app, you have it. You can have your app both embedded in facebook and in your site at once, see:
http://onboard.yoz.sk/ vs. http://apps.facebook.com/onboard_yoz_sk/ (embedded)
…before your app can use something from facebook api, you must support facebook connection (main facebook flash client does it in new window)…
for extended permissions read these:
http://blog.yoz.sk/2010/01/facebook-extended-permissions-with-authorization-by-overriding-class-in-swc/
http://blog.yoz.sk/2010/01/elegant-facebook-login-for-desktop-application/
good luck
Sorry, while posting I have an 210 error. Do anyone know what is it?
And what can I do? All permissions are granted…
mb smth in app settings?
hi CorC, try using
media.type = “image”
Thanks for this very enlightening seeing as the PublishPost is not mentioned anywhere in the online documents.
hi thanks for the snippet
i am posting the message
is show succesful in call.addEventListener(FacebookEvent.COMPLETE,onComplete)
event
but am not able to see my posted message in facebook site
@girish if complete event was dispatched your message should have been posted, keep refreshing facebook – it sounds like a facebook issue
Jozef,
First of all, thanks for the blog. This blog is literally the only source of really solid, reliable, trustworthy information I can find for making Flash games that interface with Facebook. Keep it coming, your stuff is fantastic.
I am struggling with making a Flash app do feed posts. I believe that you’re incorrect when you say that the AS3 API command “com.facebook.commands.stream.PublishPost” is an exact analog for the FBJS API method “streamPublish”: http://developers.facebook.com/docs/fbjs/streamPublish
…Because the streamPublish method appears to NOT require the publish_stream extended permission! …Whereas, as you note above, this permission IS required by the PublishPost command in AS3 (which I believe is actually the analog for stream.publish: http://developers.facebook.com/docs/reference/rest/stream.publish)
…Okay, in Facebook’s horrible documentation, I just found that streamPublish’s behavior also exists in FB.Connect.streamPublish: http://developers.facebook.com/docs/reference/oldjavascript/FB.Connect.streamPublish
However, unless I’m very confused (and note that I am), both of these can only take place on the JavaScript layer – I can’t trigger these directly from the AS3 API. (Which makes sense in a way, since they launch all these feed-forms, and I’ve never yet seen anything that I’ve called from AS3 that has properly launched a feed-form? But… I wish that did work.
Basically I want to, without requiring the user to grant publish_stream permission, and IF POSSIBLE without using any JavaScript, allow my app to initiate a feed publish, preferably by bringing up a standard feed-form pop-up.
Can I do this solely with the AS3 API? Or should I give up on that and go to using the FBJS bridge? I’m not familiar with how that works but I can switch to it if necessary… I would greatly value your advice Jozef!!!
Hi @IQpierce
thank you for appreciation my work
This article was written in the old REST API era. Now I recomment you to switch to OAuth/Graph API:
http://blog.yoz.sk/facebookoauthgraph/
The whole thing with facebook permissions is to inform user what is going on with his account. If you use javascript sdk to publish posts, the default facebook popup appears that user already know, and he has control over changing text etc. While using javascript sdk, or popup, or anythink that is created by facebook (plugins/widgets) and uses its common ui, you do not need to ask user for permissions… But if you use direct api call via client side or server side and permissions did not existed, any application could spam that users wall without noticing.
In order to open standard facebook publish stream lightbox (popup within the page) you have to use facebook javascript sdk:
http://developers.facebook.com/docs/reference/javascript/FB.ui (my best guess while I do not use it)
In order to open standart publish stream popup (new window) use sharer.php:
http://www.facebook.com/share/
http://developers.facebook.com/docs/share (read section Creating Your Own Share URL)
In order to publish stream from actionscript using new graph api you need publish_stream permission:
http://developers.facebook.com/docs/api (section Publishing to Facebook)
Man this is complex. Thanks for the reply Jozef…
Two concerns about using the Graph API:
1. I forgot to mention that my app is an IFrame Canvas app… and I recently read that Facebook recommends that Canvas app developers continue to use the old REST API: http://developers.facebook.com/docs/guides/canvas/ … Do you still recommend that I attempt to go through the new Graph API?
2. Again, I’m trying to do as much as possible entirely within AS3, and trying to use Adobe’s AS3 API for Facebook (http://code.google.com/p/facebook-actionscript-api/). This currently appears to only support the old REST API… and also appears to be poorly maintained (though there have recently been a couple of posts from an Adobe representative stating that it was going to be revised to support the Graph API and re-released in September). Do you have a reliable way to make calls against the Graph API directly from AS3? Is there some alternative to the AS3 API that’s just as easy-to-use and supports Graph?
Thanks again so much for all the great information you’re sharing here Jozef, I appreciate ANY help you can give on my questions!
Hi @IQpierce, you are welcome!
You can use iframe with the new graph api, just go through articles listed in here: http://blog.yoz.sk/facebookoauthgraph/ … REST API may become obsolete very soon (noone knows when exactly), you should really switch to the new one.
You are right, the original adobe facebook actionscript api is poorly maintained, latest update is more than 7 months old. The login and calls are far more easier with new oauth2 and graph than it used to be with adobe library, just try FacebookOAuthGraph lib to see the difference
i (re)start developing facebook apps with flash and your blogs helps very well. thanx for that!
but now I have a question: I have an image in my swf which I want to post via stream.Publish, but I don’t want to save it anywhere. I want to send it as bytearray/binary data to the wall of a friend. dou you know any way to do this or is this impossible?
I thought of two possible ways:
1. using your great Class with facebook.call(“method/stream.publish”, data, …); – but what should I put into media.imgsrc=…?
2. sending the bytearray to the DOM via javascript and then use the FBJS to send it to the users wall. I don’t know if that works (maybe this helps: http://www.adamia.com/blog/high-performance-javascript-port-of-actionscript-byteArray)
could you or anybody else help me out here?
thanx!!!!
maybe this one helps, too: http://blog.dannypatterson.com/?p=250
for diskussion, i opened a forum stream, too: http://forum.developers.facebook.net/viewtopic.php?pid=266991#p266991
Hi @HK, as far as I know facebook API stream attachments http://developers.facebook.com/docs/guides/attachments only lets you posting href (url) not the data directly. What is interesting facebook pulls the content from the url periodically (once a week or so). You can use http://developers.facebook.com/docs/reference/rest/photos.upload method (REST api) to upload raw bytes of photo into your album.
i strongly recommend to swith your app into new Graph API, while old REST API may become obsolete very soon.
@Jozef Chúťka: Hi! Thanx for answering! I use the FacebookOAuthGraph.as and don’t build on old REST.
So is there no hope for posting images on friends walls without pre-saving the image somewhere?
@HK I do not think so, you have to use href parameter – URL
@Jozef Chúťka: what about sending the raw data to JS? it would be great to be independant from serversiede scripts.
@HK based on the docs there is no way to do that, no client side “hack” will do the thing while facebook robot reads the content from the url! You have to save your content to server side before publishing it.
Hi Jozef,
Thanks a lot for your AS3 class, your support and availability, by chance you are here to fill the blanks let by Adobe (and they are huge now..).
Of course I have a question.
I want to publish a post with attachment (your example works perfectly(), but I would like to post the message to multiple friends ids in one call.
I can make your solution work by calling the method recursively as many times as I have friends (pre seleted by the user prior to that of course), one big problem remains : if the sender choose to notify N friends, he will get N notifications on his wall !!
Do you know if this is possible to do that with old REST API (that I use according to your example)?
In the documentation here :
http://developers.facebook.com/docs/reference/api/post
FB gives as an example https://graph.facebook.com/19292868552_118464504835613 in which we have :
“to”: {
“data”: [
{
"name": "f8",
"category": "Technology",
"id": "11204705797"
}
]
}
Would it be the good way to do it ?
Biggest problem again is how posting message with attachment using the new GRAPH API..
Thanks a lot (again)
Ben
@Ben, yes you can publish stream to any profile you want (your friends, your pages etc.), you need to define target_id with your publish request:
new PublishPost(message, attachment, [link], target_id);
with FacebookOAuthGraph ( http://blog.yoz.sk/facebookoauthgraph/ ), your call would look something like this:
var data:URLVariables = new URLVariables();
data.message = “some stuff”;
data.target_id = 123456; // numeric id or nickname
call(“method/stream.publish”, data, URLRequestMethod.POST, null, “https://api.facebook.com”);
using pure graph api with attachment:
http://blog.yoz.sk/2010/06/extending-facebookoauthgraph-class/ find “Publishing feed using Graph API”
Hi Jozef,
Thanks for you answer.
My concern was more about publishing to multiple target ids at a time.
Publishing to one target with attachment works fine (ta for the links).
I am looking for something like:
target_ids = “123456,456789,123789″;
To make one post to multiple friends.
Thanks Jozef.
Ben
@Ben, impossible with current api
Good to know Jozef.
Thanks a lot !
can you put full code, i don’t know how to call this function im beginer
Hi @Hadis, if you are a beginner, please go through http://blog.yoz.sk/facebookoauthgraph/ articles and use FacebookOAuthGraph library
hi, do you have a working folder for this? because i got errors like 1046 when i tried putting the codes in a function. im new to this so kind of lost.
Hi nat,
what version of facebook library do you use? the adobe one or FacebookOAuthGrap, do you receive facebook or flash player error 1046? is it while compile time or a runtime? Is it when trying to make request with publish post?
If you are using old adobe library, please switch to FacebookOAuthGraph – http://blog.yoz.sk/facebookoauthgraph/
Hi Jozef,
Your blog has helped me alot with integrating facebook into out app. Sorry if this is the wrong area but its the most relevant post I could find.
We’re using flex 4 and the Facebook flex 3.4 API:
We use the uploadPhoto method to upload a photo to the users profile this seems to work great. We’d like to be able to tag the user, or something else in the photo. Do you know if this is possible…
Thanks in Advance
im using the adobe version and the errors are runtime. thanks alot. i guess i’ll look around at the website you’ve given. anyway it works for actionscript 3.0 right?
Hi @Matthew, yes you can tag user with REST API
http://developers.facebook.com/docs/reference/rest/photos.addTag
@nat, I guess, this error is thrown when there is an undefined variable you are trying to acces property of… may it be facebook.uid?
yup thats true there’s undefined variable. im not good at this actually. doing it for school project. =/
The link you’ve posted hasn’t really help as it has no code examples for the Flex API. I’ve been up and down the site and left comments on the forums but no answers yet…
Have you got an example of code on how to tag someone using AS3 or Flex?
This is a short excerpt of my code:
public function set setImage(_image:BitmapData):void
{
var _enc:JPEGEncoder= new JPEGEncoder( 80 );
var _snapshotBA:ByteArray = _enc.encode( _image );
m_image=_snapshotBA;
}
private function uploadImage( _event:Event ):void
{
//Creates the photo and attaches the image to it
var _uploadPhoto:UploadPhoto = new UploadPhoto(m_image);
//Uploads the Photo
var _uploadPhotoPost:UploadPhoto = m_fbook.post(_uploadPhoto) as UploadPhoto;
//Creates an event listener on the Uploading process, that fires once the photo is uploaded successfully.
_uploadPhotoPost.addEventListener(FacebookEvent.COMPLETE, onUploadPhotoComplete,false,0,true);
}
Sorry if I’m barking up the wrong tree here, but been scouring the web and I’m finding very little recent documention on the AS3/Flex Facebook api.
If you can’t help no prob, Thanks for your time anyway
@Matthew, this is a part of code that worked for me:
public function uploadPhoto(byteArray:ByteArray):void
{
var uploadPhoto:UploadPhoto = new UploadPhoto(byteArray, null, “hallo world”, facebook.uid);
var call:FacebookCall = facebook.post(uploadPhoto);
call.addEventListener(FacebookEvent.COMPLETE, uploadPhotoComplete);
}
private function uploadPhotoComplete(event:FacebookEvent):void
{
if(!event.success)
return;
var data:FacebookPhoto = FacebookPhoto(event.data);
if(!data.pid)
return;
var call:FacebookCall = facebook.post(new AddTag(data.pid, facebook.uid, “”, 50, 45));
call.addEventListener(FacebookEvent.COMPLETE, tagPhotoComplete);
}
@ Jozef: SWEEEEEEET Works perfectly!
Thanks for the help, the site rocks!
is this example still up to date or should i use this new oath stuff?
@tom I recommend you to switch to the new API with all your new projects
Hi Jozef,
When asking for extended permissions for the uploading of a photo, we’d use:
m_fbook.grantExtendedPermission(“PHOTO_UPLOAD”);
In firefox (other browsers as well maybe) the permissions window open in a new tab. Is there any way to do this in a popup rather using the ActionScript?
At the moment we’re resorting to using the following javascript code to achieve the popup:
function showPermissionsDialog()
{
var cb = function(response) {
Log.info(‘FB.login callback’, response);
if (response.session) {Log.info(‘User logged in’);
if (response.perms)
{
Log.info(‘User granted permissions’);
}
}
else
{
Log.info(‘User is logged out’);
}
};
FB.login(cb, {perms: ‘photo_upload’});
}
Hi Matthew, I recoomend you to switch to FacebookOAuthGraph http://blog.yoz.sk/facebookoauthgraph/
You can open the permission window using ExternalInterface and javascript
I am getting error while publishing the post in facebook.
From as3 how to publish the post? i’m getting the error like uid or sessionid is required.
if i pass the uid, i am getting the error as user has not authorised the application.
How to rectify it?
Can anyone help me?
@eternity, first you need to get authorized, please read article http://blog.yoz.sk/2010/05/facebook-graph-api-and-oauth-2-and-flash/
Hi Jozef,
Concering the Graph method of posting, I see fb has added support for actionLinks: http://developers.facebook.com/blog/post/420.
Our publishing worked great before using Flash Builder 4, Facebook Graph AS3 API and the following syntax:
Code:
Facebook.postData
(
_method,
publishPostResult,
{
message:_messageObject.message,
name:_messageObject.attachment.name,
link:_messageObject.attachment.href,
source:_messageObject.attachment.media.src,
caption:_messageObject.attachment.caption
}
);
So I have tried adding action links using:
Code:
Facebook.postData
(
_method,
publishPostResult,
{
message:_messageObject.message,
name:_messageObject.attachment.name,
link:_messageObject.attachment.href,
source:_messageObject.attachment.media.src,
caption:_messageObject.attachment.caption,
actions:{name:_messageObject.action_links.text, link:_messageObject.action_links.href}
}
);
and numerous other ways. but i keep getting the following error:
OAuth “Facebook Platform” “invalid_request” “(#100) actions should be a JSON-encoded dictionary with \”name\” and \”link\” keys”
Any help would be greatly appreciated.
Hi Matthew,
action links works like this:
attachment.properties = {prop1:{text: “value 1″, href:”http://”}, prop2:…};
data.attachment = JSON.encode(attachment);
for more read
http://blog.yoz.sk/2010/06/extending-facebookoauthgraph-class/
Hey Jozef,
Once again, best place on the net for answer in this respect
Got it working like this:
var _actionLinks:Object=new Object();
_actionLinks = {name:”Name of Link”,link:”http://somthing”};
}
//Has Permissions, Post automatically:
Facebook.postData
(
_method,
publishPostResult,
{
message:”Actual Message”,
name:”Title”,
link:”http://…”,
source:”Image.jpg”,
caption:”capion”,
actions:JSON.encode(_actionLinks)
}
);
Wouldn’t of gotten there without Your help,
Thank You
@Matthew, thank you
it seems like there has been some changes, its good to have it listed here:
previously
data.attachment.properties vs. now data.actions
Hi Jozef,
how are you? I have a question for you about f-book. For the moment I have suspended the project of the depth map, I will go back on that later.
I have to post a swf file in the wall including a title and a short message.
I have set the permissions during the session login:
“read_stream,publish_stream,user_photo_video_tags,friends_photo_video_tags,user_photos,friends_photos,email,manage_pages,friends_about_me,user_about_me,read_friendlists,user_videos”
I try making a request like this:
var parameters:Object = new Object();
parameters.message = “bla bla bla…”;
parameters.actions = {“name”:”View”,”link”:http://www.zo….“};
parameters.source = {“file.swf”};
var fql:String = “/”+12345+”/feed”;
Facebook.api(fql,handleFeedback,parameters,”POST”);
At the end nothing as I wanted. Do you have any suggestion? Thk
Cheers,
Filippo
Hi filippo,
I recomend you to switch to FacebookOAuthGraph library:
http://blog.yoz.sk/2010/05/facebook-graph-api-and-oauth-2-and-flash/
in that case you can use an example from http://blog.yoz.sk/2010/06/extending-facebookoauthgraph-class/ (see “Publish feed with flash”)