Facebook.Exceptions.FacebookTimeoutException: Session key invalid or no longer valid

Jul 12, 2007 at 8:24 PM
I keep getting "Facebook.Exceptions.FacebookTimeoutException: Session key invalid or no longer valid" error in my facebook test application at the line Collection<User> userinfo = fbService.GetUserInfo(fbService.UserId);

I'm using v1.1 of the toolkit:

public void Page_Load(object sender, EventArgs e)
{
Facebook.Components.FacebookService _fbService = new Facebook.Components.FacebookService();
const string FACEBOOK_APPKEY = "d290799ec863a6404e76724f11d30757";
const string FACEBOOK_SECRET = "1d5ddb08a0c5bca5d759334256270228";

fbService.ApplicationKey = FACEBOOKAPPKEY;
fbService.Secret = FACEBOOKSECRET;
_fbService.IsDesktopApplication = false;

string sessionKey = Session"facebook_session_key" as String;
string userID = Session"facebook_userId" as String;
string authToken = Request.QueryString"auth_token";

// If we have an established session, set it into our instance of the service
if (!String.IsNullOrEmpty(sessionKey))
{
_fbService.SessionKey = sessionKey;
_fbService.UserId = userID;
}
// auth_token in the query params, redirected from the Facebook login page
else if (!String.IsNullOrEmpty(authToken))
{
_fbService.CreateSession(authToken);
Session"facebook_session_key" = _fbService.SessionKey;
Session"facebook_userId" = _fbService.UserId;
Session"facebook_session_expires" = _fbService.SessionExpires;
}
// login required
else
{
Response.Redirect(@"http://www.facebook.com/login.php?api_key=" + _fbService.ApplicationKey + @"&v=1.0");
}

if (!IsPostBack)
{
Collection<User> userinfo = fbService.GetUserInfo(fbService.UserId); // error here
}

}
Coordinator
Jul 12, 2007 at 9:16 PM
does it happen everytime you call or just periodically?

The issue might be that you are pulling back a facebook session out of your session and that one in session is not valid. Try clearing your session at the beginning one time, and see if it goes through.
Jul 12, 2007 at 9:54 PM

rpowers119 wrote:
does it happen everytime you call or just periodically?

The issue might be that you are pulling back a facebook session out of your session and that one in session is not valid. Try clearing your session at the beginning one time, and see if it goes through.


Hi, the problem always occurs after I add/remove the application from my application profile. Can you clarify (code samples) of what you mean by clearing sessions?
Coordinator
Jul 13, 2007 at 1:37 PM
Is this a canvas app? If so, you can probably solve your problem by pulling the new facebook session out of the request prior to pulling it out of session. You can look at the CanvasIFrameBasePage for an example.

Otherwise, you need to capture the fact that someone Removed the app. (Which you can get them to post to a url). And then clear the Session at that point.
Session[SESSION_SESSION_KEY] = null;
Session[SESSION_USER_ID] = null;


If you can't capture the remove event. You can always catch the invalid session exception that you are getting. Clear the session. And refresh the page.
Jul 13, 2007 at 6:20 PM
Yes, it's the canvas app. Actually, the problem can be reproduced when I tried one of your apps at http://apps.facebook.com/aspnetcanvasiframe/

What I did was:
1. Add http://apps.facebook.com/aspnetcanvasiframe/ to my applications
2. Removed that application
3. Visit http://apps.facebook.com/aspnetcanvasiframe/ again and I get that error about invalid sessions.

The FacebookWebSample also has the exact same issue.


Coordinator
Jul 13, 2007 at 6:42 PM
I added an issue for this scenario. I will put a fix for this problem into my sample so that you can see how you can handle it.
Jul 28, 2007 at 5:33 AM
hi could you point me to where the fix is? I am having the same problem

thanks
Coordinator
Aug 7, 2007 at 2:58 AM
It is in the IFRameBasePage that is checked in to the latest version.
Sep 12, 2007 at 7:40 PM
Hello,

Before stating my problem I would like to say tks for you guys for the API and the discussions. Facebook is the way to go...

I am having this problem and cannot really solve it.

The error that gives me is "Session key invalid or no longer valid "
At the Line 29: MyBase.Page_Load(sender, e)

I just really need to use the setFBML function, but I cannot find the answer to this solution. Tomorrow afternoon I am suposed to do a presentation on a possible Application for a club and I really needed to have this working. If you are around London a few pints will be the payoff ofcourse!
What can I do to solve this? I cannot find the solution on the example of the IFrameBasePage, maybe it is me who is being a bit silly and tireness has took over. Please give me a hand. A bit desperate around here.

Thank you.

__________________________________________________________________________________________

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.ObjectModel
Imports Facebook
Imports Facebook.WebControls
Imports WebApplication1.User
Imports webapplication1.ClubEvent
Imports System.Data.SqlClient

Partial Public Class _Default
Inherits CanvasIFrameBasePage
Private Const FACEBOOKAPIKEY As String = "CONFIRMED API KEY"
Private Const FACEBOOK_SECRET As String = "CONFIRMED CORRECT SECRET"
Private _fbService As Facebook.Components.FacebookService = New Facebook.Components.FacebookService()

Protected Shadows Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
MyBase.Api = FACEBOOKAPIKEY
MyBase.Secret = FACEBOOK_SECRET
MyBase.Page_Load(sender, e)

If (Not IsPostBack) Then
Try
' Use the FacebookService Component to populate Friends
Dim u As Facebook.Entity.User = Me.FBService.GetUserInfo()
Dim f As Collection(Of Facebook.Entity.User) = Me.FBService.GetFriends()

Dim s As String = ""
If f.Count <> 1 Then
s = "s"
End If
lblHelloWorld.Text = "Hello " & u.Name & " you have " & f.Count & " friend" & s & "..." & Me.FBService.SessionKey
Me.FBService.SetFBML("Hello " & u.Name & " you have " & f.Count & " friend" & s)
Catch e1 As Exception
Throw
End Try
End If
End Sub
____________________________________________________________________________________________________________
Thank you in advance.
Nov 18, 2008 at 4:51 PM
I know this post is a little older now, but the solution I found is very simple and hasn't been provided so I hope it is of use to others stumbling across this error and hence this post.

I was getting this "session key invalid" error message and the reason was that my FBML C# application was using "protected void Page_Load(object sender, EventArgs e)" instead of "new protected void Page_Load(object sender, EventArgs e)".  Very easy to miss the warning message that gives this one away.