Publishing Facebook stories on browsers with popup blockers

How to use Facebook’s Stream.Publish method on browsers with popup blockers. See a demo.
What is the problem?

Recently many Facebook developers have been frustrated by popup blockers preventing the Stream Publish dialogue box from being displayed. If a user hasn’t logged in and authorised the application, dialogue boxes are shown in a popup window instead of an iframe. That’s fine for some visitors, but for those who have a popup blocker enabled, it means that they won’t see the dialogue box and therefore can’t publish a post into the stream.

The Stream Publish dialogue being displayed in an iframe.
The Stream Publish dialogue being displayed in an iframe.

The Stream Publish dialogue being displayed in a popup window. Popup blockers will prevent this from opening.
The Stream Publish dialogue being displayed in a popup window. Popup blockers will prevent this from opening.
What causes it?

Anuncios

Keegan Street How to use Facebook’s Stream.Publish method on browsers with popup blockers. See a demo.

What is the problem?

Recently many Facebook developers have been frustrated by popup blockers preventing the Stream Publish dialogue box from being displayed. If a user hasn’t logged in and authorised the application, dialogue boxes are shown in a popup window instead of an iframe. That’s fine for some visitors, but for those who have a popup blocker enabled, it means that they won’t see the dialogue box and therefore can’t publish a post into the stream.

The Stream Publish dialogue being displayed in an iframe.
The Stream Publish dialogue being displayed in an iframe.

The Stream Publish dialogue being displayed in a popup window. Popup blockers will prevent this from opening.
The Stream Publish dialogue being displayed in a popup window. Popup blockers will prevent this from opening.

What causes it?

Previously popup blockers didn’t cause any problems for the Stream Publish method because all dialogue boxes were displayed with an iframe rather than a popup window. But in mid-May Facebook discovered that this exposed users to a clickjacking security vulnerability. To address the security problem, dialogue boxes are now shown in a popup window instead of an iframe for users who are not logged in or haven’t authorised the application making the request. According to Facebook, this change is here to stay.

Technically, popup blockers shouldn’t block popup windows that are opened intentionally by the user, for example by clicking on a link that says “Publish this to your profile”. Unfortunately however, Facebook have implemented their Stream Publish method in such a way that the relationship between the user action and the popup window being opened is not direct enough. The onus is on Facebook to fix this, but in the meantime, there is a workaround.

The workaround

While not documented by Facebook, it is very easy to construct a URL for the Stream Publish dialogue box. To publish the news feed story, the user simply has to click a link pointing to this URL. Here’s one I prepared earlier. You can construct the URL with your programming language of choice, but I will demonstrate how it works with an ActionScript 3 example. (I have also provided a PHP example in the source download).

1. Defining the post parameters

First we define the parameters for the post, such as the title, link, caption and any media that we want to attach. The attachment and action_links objects follow exactly the same structure as the JSON objects used by the streamPublish method in the JavaScript SDK and the old REST API.

01.// An event handler which publishes a Facebook story when a button is clicked
02.private function clickPost(event : MouseEvent) : void {
03. // Define the stream story parameters
04. var attachment:Object = {
05. 'name' : "I've just played golf with Lexus at St Andrews.",
07. 'caption' : "Win 2 x VIP tickets to the Open Golf Championships at St Andrews. Simply find the missing golf ball to win - it's quick and fun! Play now...",
08. 'media' : [ {'type' : 'image', 'src' : 'http://www.myapp.com/assets/share_image.jpg', 'href' : 'http://apps.facebook.com/myapp'}]
09. };
10. var action_links:Object = [
11. {'text' : 'Find the Golf Ball', 'href' : 'http://apps.facebook.com/myapp'}
12. ];
13. var publishURL:URLRequest = StreamPublish.publishStoryLink('YOUR_API_KEY', attachment, action_links, TopGoals.APP_TAB_URL);
14. // Open the URL and Facebook's dialogue box will be displayed
15. navigateToURL(publishURL, "_blank");
16.}

2. Generating the URL

This static method takes the parameters we defined in the previous step and encodes them into a URL. It uses the JSON class from Mike Chambers’ as3corelib project.

01.package com.thesedays.facebook {
02. import flash.net.URLRequest;
03. import com.adobe.serialization.json.JSON;
04.
05. public class StreamPublish {
06.
07. /**
08. * Generate a URL for displaying the Stream Publish dialogue.
09. *
10. * @param api_key The API key of your application - you must create an application to call this method.
11. * @param attachment An object containing the post text, links, media, etc.
12. * @param action_links An object containing actions links of text and hyperlinks.
13. * @param next The URL to open after the user has published the post or cancelled it.
14. * @param locale The locale code to display the Facebook interface with, for example en_US, fr_FR or nl_NL. It is generally best to leave this parameter as null because then Facebook will determine the language based on the user's browser and session.
15. */
16. public static function publishStoryLink(api_key:String, attachment:Object, action_links:Object, next:String, locale:String = null) : URLRequest {
18. (locale ? '?locale=' + locale + '&' : '?') +
19. 'api_key=' + api_key +
20. '&method=stream.publish' +
21. '&channel=' + escape('http://www.facebook.com/xd_receiver_v0.4.php') +
22. '&extern=1' +
23. '&attachment=' + escape(JSON.encode(attachment)) +
24. '&action_links=' + escape(JSON.encode(action_links)) +
25. '&display=popup' +
26. '&next=' + escape(next);
27. return new URLRequest(url);
28. }
29. }
30.}

Download the source from this example.

[http://labs.thesedays.com/2010/08/11/publishing-facebook-stories]

Enhanced by Zemanta

Autor: Gabriel Catalano - human being | (#IN).perfección®

Lo importante es el camino que recorremos, las metas son apenas el resultado de ese recorrido. Llegar generalmente significa, volver a empezar!