Error: FacebookAPI.CreateSession() -> FacebookAPI.ErrorCheck(): Invalid Parameter...

Mar 7, 2008 at 6:22 PM
If you ever received this error and spend more than 5 min to figure out what's wrong and how to fix it...

My scenario was as this -> and I'm using FBML, and CanvasFBMLBasePage :
  • user clicks on application link outside of Facebook ( i.e. somewehere on my site, I have a link such as http://apps.facyfacy.com/mygreatesapp )
  • user is prompted to login to Facebook
  • my callback URL is shown! not actual Facebook Canvas page...
  • hit "Refresh" in browser nad booom - above error! FacebookAPI.ErrorCheck(): Invalid Parameter....

So, if you look closely in Facebook Documentation for Authentication process (can be found @ http://developers.facebook.com/documentation.php?doc=auth ), 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 applicaiton worked as expected.

To fix it:
  • 1) In Facebook Developer Toolkit, in Facebook.WebControls - BasePageHelper class,
in public static void LoadFBMLPage function I added the following lines:
if ( !string.IsNullOrEmpty(inCanvas) && inCanvas.Equals("1"))
{
response.Write("<fb:redirect url=\"" + FACEBOOKLOGINURL + fbService.ApplicationKey + @"&v=1.0&canvas" + "\"/>"); // <-- new added code
}
else
{
response.Write("<fb:redirect url=\"" + FACEBOOKLOGINURL + fbService.ApplicationKey + @"&v=1.0" + "\"/>"); // <-- old existing code
}
  • 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....you can see my sample app @ http://apps.facebook.com/fbtestappsiccolo , and article on how to build it @ http://www.siccolo.com/Articles/CodeProject/Facebook/facebook-application-development-1.html

Regards,
Hope this helps....

Greg
Mar 26, 2008 at 8:36 PM
Hi Greg,

I am getting the error that you see occasionally but I'm not sure if this is the right fix for me.

First, it looks like you are suggesting a change to the toolkit code, right? So any changes have to be merged when the toolkit is updated. Or do you think your changes will be incorporated into a future release?

Thanks,
Sean
Apr 19, 2008 at 1:54 PM
hi Sean,

are you using canvas, or stand-alone web/desktop app? Did you check your windows event log for other ASP.NET error(s) related to Facebook Toolkit?

Yes, change I'm suggesting is for Facebook.Webcontrols. Yes, ths fix will have be merged in a future release, unless it's included.

... more at http://www.siccolo.com/articles.asp
Apr 21, 2008 at 3:50 PM
I'm using the canvas. The thing is I don't get the error message from every user, just about 0.8% of the time. Looking at the stack trace the code fails at line 91 in BasePageHelper.cs which occurs before your changes begin so even if I applied the change it appears it would never hit it.

I checked for other errors and this is the only one remaining. I had to make changes to the API to get those errors fixed too.

Apr 21, 2008 at 3:59 PM
Oh and also your step to put: AddCanvasParameterToRequestQueryString(); in the PageLoad()...is that for every PageLoad() in the app or just the default.aspx or where? And I assume it just adds a "canvas" parameter to the Request object. It seems strange that this is being done on the PageLoad(), so it seems like you are forcing "canvas" to be in the request that you're processing regardless if it actually was in the request. I'm just trying to understand...
Apr 23, 2008 at 2:13 AM

Yes, AddCanvasParameterToRequestQueryString() function only adds "canvas" paramter to Request query string, and yes I'm "forcing" it to be in the query string:

private void AddCanvasParameterToRequest()
{
try
{
NameValueCollection queryString = Request.QueryString;
queryString = (NameValueCollection)Request.GetType().GetField("canvas", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
PropertyInfo readOnlyInfo = queryString.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
readOnlyInfo.SetValue(queryString, false, null);
queryString"canvas" = "&canvas";
readOnlyInfo.SetValue(queryString, true, null);
}
catch
{
//
}
}

No, I only do it in application main-default page. The problem was in BaseHelper class that was supposted to look for "canvas" parameter, but it didn't process it according to Facebook developer guide (see http://developer.facebook.com/documentation.php?v=1.0&doc=auth.

In your case, do you know if error occurs when user is not logged in, and trying to open your app? BaseHelper class simply checks/creates Facebook session, and if error is occuring there, then it has something to do with login into Facebook while opening your app.

You're saying it failes on line 91 - is this it:
if (!String.IsNullOrEmpty(sessionKey))
{
fbService.SessionKey = sessionKey;
....


Apr 23, 2008 at 8:55 PM
My line 91 of BasePageHelper.cs is:
            else if (!String.IsNullOrEmpty(authToken))
            {
                fbService.CreateSession(authToken);  //<-line 91
            }

I didn't know you could change the Request to not be read-only, that's cool.
I believe the error occurs occasionally when a user tries to add/install the application. So you are suggesting it could be a problem with the user account?

(Also note that double braces puts your code in unformatted mode so the brackets don't get converted by the wiki.)


siccolo wrote:

Yes, AddCanvasParameterToRequestQueryString() function only adds "canvas" paramter to Request query string, and yes I'm "forcing" it to be in the query string:

 private void AddCanvasParameterToRequest()
    {
        try
        {
            NameValueCollection queryString = Request.QueryString;
            queryString = (NameValueCollection)Request.GetType().GetField("canvas", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
            PropertyInfo readOnlyInfo = queryString.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
            readOnlyInfo.SetValue(queryString, false, null);
            queryString["canvas"] = "&canvas";
            readOnlyInfo.SetValue(queryString, true, null); 
        }
        catch
        {
            //
        }
    }

No, I only do it in application main-default page. The problem was in BaseHelper class that was supposted to look for "canvas" parameter, but it didn't process it according to Facebook developer guide (see http://developer.facebook.com/documentation.php?v=1.0&doc=auth.

In your case, do you know if error occurs when user is not logged in, and trying to open your app? BaseHelper class simply checks/creates Facebook session, and if error is occuring there, then it has something to do with login into Facebook while opening your app.

You're saying it failes on line 91 - is this it:
if (!String.IsNullOrEmpty(sessionKey))
{
fbService.SessionKey = sessionKey;
....




Apr 23, 2008 at 10:40 PM
no, I think, problem is in CreateSession() ( as it was in my case) - when user's trying to open your application, while not logged on..FacebookToolkit/Facebook redirects user to login page and then....booom...

Did you try to implement my fixes?
Apr 24, 2008 at 3:35 PM
I did implement your fixes and stopped seeing the error (for one day, at least). I did get an error like this though:
Session key invalid or no longer valid at Facebook.API.FacebookAPI.ErrorCheck
Apr 24, 2008 at 5:23 PM
weird...there's no refernce of Session[] in internal static voice ErrorCheck() function, in FacebookApi.cs.... did it give you line #?
Apr 24, 2008 at 11:16 PM
Edited Apr 25, 2008 at 1:22 AM
Here's the entire stack trace:

Session key invalid or no longer valid at Facebook.API.FacebookAPI.ErrorCheck(XmlDocument doc) in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 2220 at Facebook.API.FacebookAPI.LoadXMLDocument(String rawXML) in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 2109 at Facebook.API.FacebookAPI.IsAppAdded() in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 612 at Facebook.Components.FacebookService.IsAppAdded() in C:\Work\f8toolkit\branches\Sean\Facebook\Components\FacebookService.cs:line 1775 at Facebook.WebControls.BasePageHelper.LoadFBMLPage(FacebookService fbService, Boolean autoAdd, HttpRequest request, HttpResponse response) in C:\Work\f8toolkit\branches\Sean\Facebook.WebControls\BasePageHelper.cs:line 107 at Facebook.WebControls.CanvasFBMLBasePage.PageLoad(Object sender, EventArgs e) in C:\Work\f8toolkit\branches\Sean\Facebook.WebControls\CanvasFBMLBasePage.cs:line 44 at BottlenotesClub.Facebook.FBMLPage.PageLoad(Object sender, EventArgs e) at BottlenotesClub.Facebook.Register.Page_Load(Object sender, EventArgs eventArgs) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)Facebook

Let me know which files you're interested in...
Apr 25, 2008 at 3:23 PM
Got another Invalid parmaeter error again:

Invalid parameter at Facebook.API.FacebookAPI.ErrorCheck(XmlDocument doc) in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 2234 at Facebook.API.FacebookAPI.LoadXMLDocument(String rawXML) in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 2109 at Facebook.API.FacebookAPI.ExecuteApiCall(IDictionary`2 parameterDictionary, Boolean useSession) in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 2085 at Facebook.API.FacebookAPI.CreateSession() in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 1947 at Facebook.API.FacebookAPI.CreateSession(String authToken) in C:\Work\f8toolkit\branches\Sean\Facebook\API\FacebookAPI.cs:line 138 at Facebook.Components.FacebookService.CreateSession(String authToken) in C:\Work\f8toolkit\branches\Sean\Facebook\Components\FacebookService.cs:line 241 at Facebook.WebControls.BasePageHelper.LoadFBMLPage(FacebookService fbService, Boolean autoAdd, HttpRequest request, HttpResponse response) in C:\Work\f8toolkit\branches\Sean\Facebook.WebControls\BasePageHelper.cs:line 91 at Facebook.WebControls.CanvasFBMLBasePage.PageLoad(Object sender, EventArgs e) in C:\Work\f8toolkit\branches\Sean\Facebook.WebControls\CanvasFBMLBasePage.cs:line 44 at BottlenotesClub.Facebook.FBMLPage.PageLoad(Object sender, EventArgs e) in C:\Work\dotnet2a\wine-clubs\Facebook\FBMLPage.cs:line 38 at BottlenotesClub.Facebook.Default.Page_Load(Object sender, EventArgs eventArgs) in C:\Work\dotnet2a\wine-clubs\Facebook\Default.aspx.cs:line 62 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)Facebook
Jun 2, 2008 at 8:11 AM
Hi,
I have the same problem. Can anyone put compilated dll?
Thanks!
Jun 3, 2008 at 6:22 AM
Same problem. Here are my details.