Title:       Facebook Application Development - Error - FacebookAPI.CreateSession() - FacebookAPI.ErrorCheck() - Invalid Parameter Fix
Author:      Greg Dubinovskiy 
Email:       [email protected]
Environment: c# .NET
Keywords:    Facebook Application Development - Error - FacebookAPI.CreateSession() - FacebookAPI.ErrorCheck() - Invalid Parameter Fix
Level:       Intermediate
Description: Facebook Application Development - Error - FacebookAPI.CreateSession() - FacebookAPI.ErrorCheck() - Invalid Parameter Fix
Section      Miscellaneous
SubSection   General

Siccolo Development Articles - Facebook Application Development - .NET Software Development - Generate Facebook Content using FBML - using FBML - using FBJS

After my greatest Facebook application was sort-of developed, it was time for testing... Ha-ha! What do I know - it didn't work! Messed with various settings, still wouldn't work! Drove me bananas...

If you ever received error Invalid Parameter and spend more than 5 min to figure out what's wrong and how to fix it...

If you look closely in Facebook Documentation for Authentication process (can be found @ Facebook Application Authentication ), there is canvas parameter that can be passed when redirecting to Facebook login.php. And after experementing with actual URL in browser address, and manually adding canvas parameter in browser URL address, my Facebook application worked as expected.

    To fix it:
  1. In Facebook Developer Toolkit, in Facebook.WebControls - BasePageHelper class:
    internal class BasePageHelper
    {
    	...
    	...
    	public static void LoadFBMLPage(FacebookService fbService, bool autoAdd, HttpRequest request, HttpResponse response)
            {
                string sessionKey = null;
                string userId = null;
                string inCanvas = request[REQUEST_IN_CANVAS];
    
                fbService.IsDesktopApplication = false;
                            
                if (string.IsNullOrEmpty(fbService.SessionKey) || string.IsNullOrEmpty(fbService.UserId))
                {
                    sessionKey = request[REQUEST_SESSION_KEY];
                    userId = request[REQUEST_USER_ID];
                }
                else
                {
                    sessionKey = fbService.SessionKey;
                    userId = fbService.UserId;
                }
    
                // When the user uses the facebook login page, the redirect back here will will have the auth_token in the query params
                string authToken = request.QueryString[QUERY_AUTH_TOKEN];
    
                // We have already established a session on behalf of this user
                if (!String.IsNullOrEmpty(sessionKey))
                {
                    fbService.SessionKey = sessionKey;
                    fbService.UserId = userId;
                }
                //// This will be executed when facebook login redirects to our page
                else if (!String.IsNullOrEmpty(authToken))
                {
                    fbService.CreateSession(authToken);
                }
                else
                {
                    //response.Redirect(@"http://www.facebook.com/login.php?api_key=" + fbService.ApplicationKey + @"&v=1.0");
                    //check if canvas!
                    if ( !string.IsNullOrEmpty(inCanvas) && inCanvas.Equals("1"))
    		{
                        response.Write("<fb:redirect url=\"" + FACEBOOK_LOGIN_URL + fbService.ApplicationKey + @"&v=1.0&canvas" + "\"/>");
                    }
                    else
                    {
                        response.Write("<fb:redirect url=\"" + FACEBOOK_LOGIN_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
                    }
                    response.End();
                }
    
                if (!fbService.IsAppAdded() && autoAdd)
                {
                    if ((!string.IsNullOrEmpty(inCanvas) && inCanvas.Equals("1")) || (!string.IsNullOrEmpty(request.Url.ToString()) && request.Url.ToString().ToLower().Contains("facebook.com")))
                    {
                        response.Write("<fb:redirect url=\"" + FACEBOOK_ADD_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
                        response.End();
                    }
                    else
                    {
                        response.Redirect(FACEBOOK_ADD_URL + fbService.ApplicationKey + @"&v=1.0");
                    }
                }
    	}
    }
    
    Bellow is added/changed code:
    if ( !string.IsNullOrEmpty(inCanvas) && inCanvas.Equals("1"))
    {
    	response.Write("<fb:redirect url=\"" + FACEBOOK_LOGIN_URL + fbService.ApplicationKey + @"&v=1.0&canvas" + "\"/>");
    }
    else
    {
    	response.Write("<fb:redirect url=\"" + FACEBOOK_LOGIN_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
    }
    
  2. compiled Facebook.WebControls. great.
  3. now, I needed to add canvas parameter to QueryString in my application:
    public partial class _App : CanvasFBMLBasePage 
    {
    	...
    	....
    	new protected void Page_Load(object sender, EventArgs e)
    	{
    		AddCanvasParameterToRequestQueryString();
    		base.Api = FACEBOOKAPIKEY;
    		base.Secret = FACEBOOK_SECRET;
    		base.Page_Load(sender, e);
    		...
    		....
    	}
    }
    




    And it works now.... And you can see this sample application on Facebook - Traveling Babies




    Article keywords: Facebook, FBML, FBJS, AJAX, Mock AJAX , fb:action, fb:create-button, fb:help, fb:dashboard, fb:friend-selector, fb:editor, fb:iframe, , Animation, setTextValue, setValue , getElementById, span, div, ajax.post, , asp:DropDownList, asp:Button, asp:GridView, form, runat=server, server-side, DataSet, DataColumn, DataRow, GridView, DataSource

    Back To Articles Page

    Free Mobile Management For SQL Server(s!) - Siccolo - SQL Management ToolQuestions? Suggestions? Concerns? - email me to [email protected]    Greg Dubinovsky © 2008
    or share your thoughts at Siccolo Blog

    Web being sponsor - Mid-Atlantic Processing. Well being sponsor - Clarity MediSpa. Hairless sponsor - Clarity MediSpa Laser Hair Removal.
    Siccolo - SQL Server Management Tool For Mobile Devices is packed with built-in functionality and tools. Siccolo delivers a rich set of management tools for both DBAs and sys admins. SQL Server management has always been an area of DBA concern. The new Management Tool For Mobile Devices - Siccolo - has simple "Enterprise Manager" and the "Query Analyzer". Siccolo is a management tool for the MS SQL Server with administration capabilities and a database query tool. The administration features provide users the ability to browse database structures. An integrated query tool allows users to quickly create, edit and execute SQL queries and scripts. Siccolo also provides an export tool to allow users to easily save and email execution results. Siccolo helps database professionals save time and increase their productivity by utilizing a more efficient approach to database management - use their Windows Mobile empowered device while sipping margarita on the beach For increased security, Siccolo is configured to run under SSL with IIS authentication.
      Siccolo features are:
    • Run SQL Query window (with semi-automated Insert/Update/Delete/Select Statements) Open/Save SQL Scripts in multiple tabs Save/Email results
    • Explore SQL Server Browse databases and database objects (tables/views/stored procedures) Browse/Manage SQL Server jobs Browse/Manage windows services (start/stop) View event logs (Application, System)
    • Manage SQL Server - restart MS SQL Server service and SQL Agent