Facebook PublishPost with image (update)

facebookPublishPost

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

24 comments so far

  1. Amir December 17, 2009 00:56

    Thanks, very helpfull post

  2. Gabriell December 17, 2009 21:43

    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?

  3. Jozef Chúťka December 17, 2009 22:05

    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/

  4. Poudib January 18, 2010 22:35

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

  5. Jozef Chúťka January 19, 2010 00:27

    Hi Poudib, do you use the latest facebook flex api? which version do you use?

  6. paul March 16, 2010 12:53

    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.

  7. Tommy Andersen March 18, 2010 11:28

    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?

  8. Jozef Chúťka March 18, 2010 14:18

    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

  9. CorC March 25, 2010 16:40

    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?

  10. Jozef Chúťka March 28, 2010 13:37

    hi CorC, try using
    media.type = “image”

  11. Stephen Matthews May 14, 2010 17:58

    Thanks for this very enlightening seeing as the PublishPost is not mentioned anywhere in the online documents.

  12. girish patil July 26, 2010 08:19

    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

  13. Jozef Chúťka July 26, 2010 11:17

    @girish if complete event was dispatched your message should have been posted, keep refreshing facebook – it sounds like a facebook issue

  14. IQpierce August 24, 2010 18:41

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

  15. Jozef Chúťka August 25, 2010 10:47

    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)

  16. IQpierce August 25, 2010 19:29

    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!

  17. Jozef Chúťka August 26, 2010 13:14

    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

  18. HK September 8, 2010 19:15

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

  19. HK September 8, 2010 19:35

    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

  20. Jozef Chúťka September 9, 2010 10:29

    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.

  21. HK September 9, 2010 11:40

    @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?

  22. Jozef Chúťka September 9, 2010 12:06

    @HK I do not think so, you have to use href parameter – URL

  23. HK September 9, 2010 18:31

    @Jozef Chúťka: what about sending the raw data to JS? it would be great to be independant from serversiede scripts.

  24. Jozef Chúťka September 10, 2010 10:38

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

Leave a comment

Please be polite and on topic. Your e-mail will never be published.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes