Recently, we released version 4.1 of Windows Live Messenger Connect. This version brings Messenger Connect out of beta and makes it available to everyone. Angus Logan describes many of the changes we've made in his recent blog post. I also highly recommend you check out our new developer guide, which outlines how you can start building applications that integrate with Windows Live.
A common question that we've been asked has been on building desktop applications with Messenger Connect. While it's true that most of the Messenger Connect documentation focuses on web-based applications, you can still build applications for the desktop using Messenger Connect. All you need is the Messenger Connect Desktop Starter Kit, now available for download from the MSDN Code Gallery. (Look for the file named Desktop_Sample_App.zip under the v4.1 samples release.)
Before you begin using the starter kit, you need to register your application at http://manage.dev.live.com. Registration takes just a couple of minutes; you can find more detailed steps on the registration process in the Messenger Connect documentation.
When you open the starter kit, you'll find a couple of different sample projects. The best one to start with is the AuthTest project. This project provides a basic introduction into how to use the Messenger Connect authentication and consent process. For this project to compile and run correctly, open the SigninTester.cs file in the AuthTest Project. Then, locate the line:
AppInformation appInfo = new AppInformation("00000000YourClientID", "YourSecretKey", requestedScopes);
Change the values in line to contain the client ID and secret key you received when you registered your application.
When you compile and run the sample, you get a pretty basic dialog box.
Nothing too exciting, yet. But notice the http://apis.live.net/V4.1 endpoint in the text box - more on that shortly. First, let's get things going by clicking the SignIn button. This brings up the Windows Live consent dialog box.
Type in a valid Windows Live ID and password, and then click Connect. Back in our sample application, the SignIn button has changed to a Navigate button. Click the button, and the dialog updates with the top-level REST data located at http://apis.live.net/V4.1.
From here, you can navigate through the REST endpoints by copying and pasting them into the URI text box. For example, if I want to get my profile information, I would use http://profiles.apis.live.net/v4.0/cid-XXX/Profiles, where XXX is the CID the application received after I signed in.
You can get a really good sense of how the REST endpoints work in Messenger Connect using our REST Explorer: http://rex.mslivelabs.com/.
So what's going behind the scenes here? Let's close out of the dialog box and return to Visual Studio. Open up the SignInTester.cs file. This is the file that's doing most of the work. Two methods stand out in particular: SigninSynchronous and btnNavigate_Click.
void SigninSynchronous()
{
user1 = new MessengerConnectSigninContext();
Collection<span style="font-family: consolas; font-size: 9.5pt"><span style="color: #2b91af"Scope/span> requestedScopes = new Collection();
requestedScopes.Add(Scope.ContactsView);
AppInformation appInfo = new AppInformation("00000000YourClientID", "YourSecretKey", requestedScopes);
user1.GetToken(appInfo);
EnableMessengerConnectBrowsing();
}
The SigninSynchronous method fires when the user clicks the Signin button. The really interesting part here is the scopes collection:
Collection<span style="line-height: 115%; font-family: consolas; font-size: 9.5pt"><span style="color: #2b91af"Scope/span> requestedScopes = new Collection();
This line makes it easy to add the scopes that control what Windows Live data you can access on a user's behalf. In the starter kit, we're getting the WL_Contacts.View scope from a pre-defined constant, as shown in the line:
requestedScopes.Add(Scope.ContactsView);
If we wanted to request additional scopes, we'd just need to call requestedScopes.Add again for each additional scope. Check out the SDK for more information on what scopes are available, and what they do.
Once we have the scopes we want, it's just a matter of creating a new AppInformation object that contains your application's client ID, secret key, and requested scopes. The GetToken method handles opening the Windows Live Consent dialog box and processing the access token we get back when the user signs in successfully.
The btnNavigate_Click method is very similar to what you would do if you were writing a web application.
private void btnNavigate_Click(object sender, EventArgs e)
{
try
{
Uri someUri = new Uri(txtUri.Text);
WebRequest webRequest = HttpWebRequest.Create(someUri);
webRequest.Method = "GET";
webRequest.Headers[MessengerConnectConstants.AuthorizationHeader] = user1.AuthorizationToken;
WebResponse webResponse = webRequest.GetResponse();
StreamReader responseStreamReader = new StreamReader(webResponse.GetResponseStream());
String response = responseStreamReader.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
StringBuilder formattedBuilder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(formattedBuilder, settings);
doc.WriteTo(writer);
writer.Close();
txtOutput.Text = formattedBuilder.ToString();
}
catch (Exception exception)
{
txtOutput.Text = exception.Message + "\r\n" + exception.StackTrace;
}
}
As you can see, it takes a URI and creates a WebRequest using the access token of the Windows Live user. As this is a pretty simple example, the method just writes the response back out to the dialog box. This is where you can leverage the REST Explorer and the Windows Live SDK to build more functionality into the sample.
We hope this starter kit gives you some good insight on how to build desktop applications with Messenger Connect. We're working hard on providing more information on how to code with Messenger Connect - meanwhile, if you have questions or feedback, please leave a comment.
More...