Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone Live SDK API - get new session object after restarting the App

so I have succeeded in connecting my Windows Phone 8 Application to the Live API, I also succeeded in reading data from my hotmail account.

I have access to the needed client ID and the live access token.

But when I quit and restart my application, I lose all references to the session and the client objects and I have to start the process anew.

I don't want to annoy the user with the web mask in which he has to agree again that he provides me with the needed permissions every time he is starting the application. But I also haven't found a way to get a reference to a session object without this step.

The login mask is only shown the first time after installing the application, after that, only the screen mentioned above is shown. But it is still quite annoying for the user to accept this every time.

I already tried serializing the session object, which is not possible, because the class does not have a standard constructor.

Maybe it is possible to create a new session by using the live access token, but I haven't found a way to do so.

Any ideas? What am I doing wrong, I know that there is a way to login again without prompting the user. I'm thankful for every idea.

Some code I use:

    /// <summary>
    /// This method is responsible for authenticating an user asyncronesly to Windows Live.
    /// </summary>
    public void InitAuth()
    {
        this.authClient.LoginCompleted +=
            new EventHandler<LoginCompletedEventArgs>(this.AuthClientLoginCompleted);

        this.authClient.LoginAsync(someScopes);
    }

    /// <summary>
    /// This method is called when the Login process has been completed (successfully or with error).
    /// </summary>
    private void AuthClientLoginCompleted(object sender, LoginCompletedEventArgs e)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            LiveConnector.ConnectSession = e.Session; // where do I save this session for reuse?
            this.connectClient = new LiveConnectClient(LiveConnector.ConnectSession);

            // can I use this access token to create a new session later?
            LiveConnector.LiveAccessToken = LiveConnector.ConnectSession.AccessToken;

            Debug.WriteLine("Logged in.");
        }
        else if (e.Error != null)
        {
            Debug.WriteLine("Error signing in: " + e.Error.ToString());
        }
    }

I have tried to use the LiveAuthClient.InitializeAsync - method to login in background after restarting the application, but the session object stays empty:

// this is called after application is restarted
private void ReLogin()
{
    LiveAuthClient client = new LiveAuthClient(LiveConnector.ClientID);
            client.InitializeCompleted += OnInitializeCompleted;
            client.InitializeAsync(someScopes);
}


private void OnInitializeCompleted(object sender, LoginCompletedEventArgs e)
    {
        Debug.WriteLine("***************** Inititalisation completed **********");
        Debug.WriteLine(e.Status); // is undefined
        Debug.WriteLine(e.Session); // is empty
    }

Does anyone have an idea how I could get access to a new session after restarting the application?

like image 775
FloppyNotFound Avatar asked Nov 03 '12 18:11

FloppyNotFound


2 Answers

Been struggling to get this working on a Windows Live + Azure Mobile Service app myself so thought I would post a complete working code sample here now that I've got it working.

The key parts are the wl.offline_access scope and the call to InitializeAsync.

Note: this sample also connects with Windows Azure Mobile Services. Just remove the stuff related to MobileService if you're not using that.

    private static LiveConnectSession _session;
    private static readonly string[] scopes = new[] {"wl.signin", "wl.offline_access", "wl.basic"};

    private async System.Threading.Tasks.Task Authenticate()
    {
        try
        {
            var liveIdClient = new LiveAuthClient("YOUR_CLIENT_ID");
            var initResult = await liveIdClient.InitializeAsync(scopes);

            _session = initResult.Session;


            if (null != _session)
            {
                await MobileService.LoginWithMicrosoftAccountAsync(_session.AuthenticationToken);
            }

            if (null == _session)
            {
                LiveLoginResult result = await liveIdClient.LoginAsync(scopes);

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    _session = result.Session;
                    await MobileService.LoginWithMicrosoftAccountAsync(result.Session.AuthenticationToken);

                }
                else
                {
                    _session = null;
                    MessageBox.Show("Unable to authenticate with Windows Live.", "Login failed :(", MessageBoxButton.OK);
                }
            }
        }
        finally
        {
        }
    }
like image 37
Fredrik Kalseth Avatar answered Oct 05 '22 12:10

Fredrik Kalseth


After two full days searching for the mistake I was making, I finally found out what I was doing wrong: I have to use the wl.offline_access scope to make this work!

Let me quote another user here:

"If your app uses wl.offline_access scope than the live:SignInButton control saves it for you and loads it automatically. Just use the SessionChanged event to capture the session object. This way the user will need to sign in only once." (see WP7 how to store LiveConnectSession during TombStoning?)

Now everything is fun again. Can't believe that this was the problem. Tested & working. Nice!

like image 127
FloppyNotFound Avatar answered Oct 05 '22 10:10

FloppyNotFound