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.

Leave a comment