Wednesday, July 16, 2008

More Facebook Programming - Application Login Issues

Alright Facebook programmers....
I developed my first facebook app, only to be stumbled by an annoying Facebook issue. First off, FB requires users to be logged into applications in order to use them. I am ok with this. It helps log statistics, etc...metrics developers shoud have access to.

My problem...

When a user would view my application (I didn't want them to be required to add it), they would login and be redirected INCORRECTLY.

I.e.
http://apps.facebook.com/appName/main.phphttp://my.appsite.com/appName/?auth_token=XXXXX

Note the "...main.phphttp://..."

Ultimately, this is due to the "next" variable in the query string on the login page, but unfortunately, it is difficult to change. There have been a few comments on this at: http://forum.developers.facebook.com/viewtopic.php?id=16199
as well as some other threads...seems to be quite an issue.

HOWEVER:
I have an alternative way around this that I find to be much easier to control!

PHP Code as follows (using the FB PHP5 library):

require_once 'facebook.php';

$appapikey = '3b2XXXXXXXX14';
$appsecret = 'aa2XXXXXXXX06';

$facebook = new Facebook($appapikey, $appsecret,
$_GET['fb_sig_session_key'].$_POST['fb_sig_session_key']);

$user = $facebook->get_loggedin_user();

if($user=="") {
//Force a login to the app with the CORRECT "next" url
$urlRedirect="http://www.facebook.com/tos.php?api_key=XXXXXXXXXX
&next=page.php&v=1.0&canvas";

$facebook->redirect($urlRedirect);
}



If the user is NOT logged into the application, the user will be asked to login and redirected to whichever page follows the "next" variable in the query string. In the above example, the user would be redirected to: http://apps.facebook.com/appName/page.php

There you have it! Enjoy, and comments are welcome!

5 comments:

B-the-T said...

The post added a few extra line breaks, so be careful when you "cut-and-paste." Happy coding!

gaiaville said...

Thanks for the code. I'm a newbee (facebook, html, js, php and just about everything else).

The code works for my app but how can I get my app added to the facebook user's side nav.

My link is: http://apps.facebook.com/friendlyphotodistort/

Thanks for any suggestions.

B-the-T said...

You have to set the Side Nav URL on the application settings. Then the user must select the box when adding the app to include a side-nav. Not much you can do programmatically though (I don't think).

gaiaville said...

Would you be willing to consult with me on my facebook app?

B-the-T said...

Sure...what do you need?