Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Windows Phone 7 Beta with Windows Live Messenger Connect

Recent Microsoft announced a major update to their Live platform. There are a number of updates that are currently in Beta including Messenger and Live Writer but what interests me is the new development platform, Messenger Connect. Go to the Windows Live developer portal for all the details. For anyone who has worked with any of the Live Id apis in the past it was difficult to get all the information you wanted. It appears that Messenger Connect is a great rationalisation of these apis and with libraries to support both rich and web client access, getting started should be easy.

Unfortunately, and for those getting into the Windows Phone space you’ll get used to this, there is no library available for accessing the Messenger Connect apis. Of course, the team has done a great job of making all the data available via REST  but honestly, that’s like trying to write a Windows Phone app in XAML when you have the power of Blend…. not something you want to have to do.

I figured it couldn’t be that hard to take the existing library for Silverlight, decompile it thanks to Reflector, update it to work for Windows Phone and we should be good to go. No so, after quite a painful day yesterday I have got a working library that’s able to authenticate and access contacts and activities (haven’t tested the other functionality yet). Unfortunately this meant not only reverse engineering the Microsoft.Live.dll but also the odata sdk library for Windows Phone (as they haven’t made the source available). Anyhow, enough complaining about the lack of source code publishing by Microsoft (please, please, please Microsoft, stop closed-sourcing these sdk libraries – all it does is frustrate us!).

So the SDK library is relatively easy to use:

- Start by adding a reference to the Microsoft.Live and System.Data.Services.Client (the one in the attached zip file, not the odata sdk version).

- Next, you’ll need a WebBrowser control on the page that you’re going to do the authentication on – this is used to display the Messenger Connect sign in prompt. The SDK library for Silverlight uses a ChildWindow but this isn’t available for Windows Phone so I’ve hacked around this for the moment. I’m sure there’s a better solution for this so feel free to modify it

- Add a button and some code to authenticate the user:

const string ClientId = "***Your-Client-ID***";
const string ClientSecret = "***Your-Secret-Key***";

LiveDataContext dataContext = new LiveDataContext();
AppInformation appInfo;

private void SignInButton_Click(object sender, RoutedEventArgs e)
{
    SignInButton.IsEnabled = false;

    try
    {
        appInfo = new AppInformation(ClientId, ClientSecret);
        appInfo.RequestedOffers.Add(new Offer()
        {
            Name = "WL_Contacts",
            Action="View"
        });
        appInfo.RequestedOffers.Add(new Offer()
        {
            Name = "WL_Activities",
            Action = "View"
        });
        appInfo.RequestedOffers.Add(new Offer()
        {
            Name = "WL_Photos",
            Action = "View"
        });
                
        this.dataContext.SignInAsync(this.SignInBrowser, appInfo, null);
    }
    catch (Exception ex)
    {
        ShowErrorDialog("Failed to sign in to Windows Live: {0}", ex.Message);
        SignInButton.IsEnabled = true;
    }
}

private void SignInCompleted(object sender, SignInCompletedEventArgs e)
{
    if (!e.Succeeded)
    {
        string authError = (e.AuthInfo.Error == null) ? string.Empty : e.AuthInfo.Error.Message;
        ShowErrorDialog("Failed to sign in to Windows Live! {0}", authError);
        SignInButton.IsEnabled = true;
    }
    else
    {
        MessageBox.Show("Welcome To Windows Live!", "Windows Live", MessageBoxButton.OK);
        this.Contacts.Visibility = System.Windows.Visibility.Visible;
    }
}

private static void ShowErrorDialog(string errMsg, params string[] paras)
{
    MessageBox.Show(string.Format(errMsg, paras), "Windows Live", MessageBoxButton.OK);
}

Essentially the process goes like this:

- the user clicks the SignInButton
- the WebBrowser control is displayed and navigates to the Messenger Connect sign in page
- the user signs in and grants appropriate privileges
- after sign in the WebBrowser is automatically hidden and the SignInCompleted event is raised

In action:

image

In the code to sign in you will have noticed that there are three Offers that have been requested. These correlate to the scopes of information that the user is going to grant this application. In this case View privileges to Contacts, Photos and Activities. To retrieve these sets of information we can kick of an odata service call.

private void FetchContactsButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
    var qry= dataContext.ContactsQuery;
    qry.BeginExecute(Response, qry);
}
void Response(IAsyncResult result)
{
    var qry = result.AsyncState as DataServiceQuery<Contact>;
    var contacts = qry.EndExecute(result);
    this.Dispatcher.BeginInvoke(() =>
    {
        this.InformationList.Items.Clear();
        foreach (Contact c in contacts)
        {
            this.InformationList.Items.Add(c.ToString());
        }
    });
}

private void FetchActivitiesButton_Click(object sender, RoutedEventArgs e)
{
    var qry = dataContext.MyActivitiesQuery;
    qry.BeginExecute(ActivitiesResponse, qry);
}

void ActivitiesResponse(IAsyncResult result)
{
    var qry = result.AsyncState as DataServiceQuery<Activity>;
    var contacts = qry.EndExecute(result);
    this.Dispatcher.BeginInvoke(() =>
    {
        this.InformationList.Items.Clear();
        foreach (Activity c in contacts)
        {
            this.InformationList.Items.Add(c.ToString());
        }
    });
}

This code illustrates retrieving the list of contacts and activities for the currently signed in user.

Hopefully this will help you build a Windows Phone application that takes advantage of the new Messenger Connect apis. Oh, before I forget, to get the sample to work you’ll need to sign up for access to the Messenger Connect apis, then register your application (use the Windows Application option). Include the ClientId and ClientSecret at the top of the MainPage.xaml.cs (first code sample above).

Messenger Connect Sample: Download Sample

Update: It’s important that you read the licensing agreements that accompany the use of the apis. Angus posted on Messenger Connect saying the following:

    1. Public APIs: Our “Public APIs” are available to all developers and third parties to access in a self-service manner. Appropriate use is governed by our Terms of Use and Terms of Service, and is monitored for abuse reported by customers. Third parties can sign-up for access through our application management tool at http://manage.dev.live.com.

Comments (9) -

  • Dimaz Pramudya

    7/26/2010 6:24:57 PM | Reply

    Hi Nick,

    Is there anyway to do the Authentication process without showing the WebBrowser control?
    Would love to see something similar to xAuth.

    //D

  • nick

    7/26/2010 6:41:22 PM | Reply

    Dimaz, no there isn't for the same reason as there wasn't for the old Live Id Web Authentication. This process is aweful and not really designed for rich client apps but seems to be the way of the future for web authentication systems (Twitter and Facebook both use a similar approach from what I know).

  • Trees

    7/26/2010 9:42:24 PM | Reply

    Hey Nick, Looks like you were having some fun there :).. wouldn't it be interesting if a similar approach opened the door to contact info in Outlook on the phone. There's quite a bit of demand for this. I'm guessing though that Outlook won't be storing any data here.. have you heard anything in your travels?

  • Trees

    7/26/2010 9:42:50 PM | Reply

    Hey Nick, Looks like you were having some fun there :).. wouldn't it be interesting if a similar approach opened the door to contact info in Outlook on the phone. There's quite a bit of demand for this. I'm guessing though that Outlook won't be storing any data here.. have you heard anything in your travels?

  • Trees

    7/26/2010 9:43:57 PM | Reply

    Hey Nick, Looks like you were having some fun there :).. wouldn't it be interesting if a similar approach opened the door to contact info in Outlook on the phone. There's quite a bit of demand for this. I'm guessing though that Outlook won't be storing any data here.. have you heard anything in your travels?

    ps: cast error posting comments from firefox, had to load ie.

  • Trees

    7/26/2010 9:44:51 PM | Reply

    soz, delete repeats - thought failure was not posting comment - same error in ie btw.

  • Paul Griffiths

    7/28/2010 1:03:15 PM | Reply

    looks nice nick, but there's no chance such an app would get certified.  the terms and conditions of Messenger only allow to use it in certain ways for PCs and for mobiles.   there are many apps that hack messenger (e.g. ebuddy) and they get away with it on non-microsoft platforms like the iphone.      but there's a reason why the only messenger for winmo6 is the official microsoft one...... they just won't certify any 'rogue' developer using the api.

    just giving you heads up so you don't waste too much efforts and get disappointed!!!!   can you hack maybe an ICQ or Y! Messenger app?  i'd love to see that!!

  • nick

    7/28/2010 3:53:42 PM | Reply

    Paul, Don't be confused about "Messenger" (as in Live Messenger) and "Messenger Connect" (as in Live Id Auth apis). At the end of the day all I'm accessing are the public apis.

  • Dissertation Help

    10/29/2010 5:32:30 AM | Reply

    Wonderful post about "Windows Phone 7 Beta with Windows Live Messenger Connect".

Pingbacks and trackbacks (1)+

Loading