facebook-csharp-sdk / facebook-winclient-sdk

Facebook SDK for Windows & Windows Phone
http://facebooksdk.net
Apache License 2.0
61 stars 139 forks source link

Facebook session expiration #53

Closed vasyaPP closed 9 years ago

vasyaPP commented 9 years ago

Hi, I have problem with session expiration time. After logging in with my windows app LoginAsync(...), I save session to local settings like this:

        string token = String.Empty;
        string expiry = String.Empty;
        string state = String.Empty;

        if(sess != null)
        {
            token = sess.AccessToken;
            expiry = sess.Expires.Ticks.ToString();
            state = session.State;
        }

        MyPlatform.SetLocalSetting(accessTokenSettingsKeyName, token);
        MyPlatform.SetLocalSetting(accessTokenExpirySettingsKeyName, expiry);
        MyPlatform.SetLocalSetting(stateSettingsKeyName, state);

And on starting app I'm trying to load session from local settings:

        var accessTokenValue = MyPlatform.GetLocalSetting(accessTokenSettingsKeyName);
        var expiryTicks = MyPlatform.GetLocalSetting(accessTokenExpirySettingsKeyName);
        var stateValue = MyPlatform.GetLocalSetting(stateSettingsKeyName);
        if (accessTokenValue.Equals(String.Empty) || expiryTicks.Equals(String.Empty))
        {
            session = null;
            AccessToken = String.Empty;
            isAuthenticated = false;
            return;
        }

        // read expiry
        DateTime expiryValue = DateTime.MinValue;
        long expiryTicksValue = 0;
        if (long.TryParse(expiryTicks, out expiryTicksValue))
        {
            expiryValue = new DateTime(expiryTicksValue);
        }

        // return true only if both values retrieved and token not stale
        if (expiryValue > DateTime.UtcNow)
        {
            session = new FacebookSession()
            {
                AccessToken = accessTokenValue,
                Expires = expiryValue,
                State = stateValue
            };
            AccessToken = accessTokenValue;
            isAuthenticated = true;
        }
        else
        {
            ResetState();
        }

It works fine, but I have noticed that session expiration time is too short (about 3 hours), and I need relogin and show login dialog to user too frequently. Is there any way to increase facebook session expiration time? On iOS and Android I don't have such problem.

vasyaPP commented 9 years ago

I have found in facebook docs, that I need exchange my token to long-live token. https://developers.facebook.com/docs/facebook-login/access-tokens