Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Getting user information with the Live Connect API for Windows Phone

From Wiki
Jump to: navigation, search

This article demonstrates how to get user information with Microsoft Live Connect APIs on Windows Phone 7. It also provides a brief overview of some of the other functionality exposed by these APIs.

WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820, Lumia 920
Compatibility
Platform(s): Windows Phone 8.0/ 7.1
Windows Phone 8
Windows Phone 7.5
Platform Security
Signing Required: Self-Signed
Article
Keywords: Live Connect
Created: somnathbanik (22 Apr 2014)
Last edited: hamishwillee (15 Jul 2013)

Contents

Introduction

In this article we will take a look how can we get started using the Live Connect API for Windows Phone Application and use the Live Services through the Live SDK. With the use of Live Connect API we can access Microsoft services like SkyDrive, Hotmail, and Messenger from our application. This article is a basic walk-through of MSDN reference to develop app using the Live SDK and to access user information from its Live account. To show the use of the Live SDK with the MSDN reference links, we will create a basic app to get user profile information using Live SDK, which will clearly help us to point the MSDN links and uses. The Live APIs provides information about the following.

  • SkyDrive - with the use of SkyDrive we can handle files and media of the user. SkyDrive provides a cloud storage, and play like a back-up storage server for the user.
  • Hotmail - the API also provides the privilege to access user’s contacts and calendar events.
  • Messenger - a live messenger can be created which can communicate with other users.
  • Windows Live ID – this authenticate users and access their Live profile information.

During the course of this article we will use the Windows Live ID API to access user’s profile information, and will point the reference articles on how we archive this.

Implementation

Let’s create an empty WP7 project. Before we start coding let’s install the Live SDK. To recognized our app we need an unique id(Client ID) , to get the unique id (Client ID) let’s go to Live Connect Developer Center , create a new application and get the Client ID. We will use the Client ID in our application to interact with the Live Services. Now we add the references Microsoft.Live.Controls and Microsoft.Live into our project. And we add the namespace in the XAML file.

xmlns:live="clr-namespace:Microsoft.Live.Controls;assembly=Microsoft.Live.Controls"

Next in the XAML file we will add the Signin Control which will help us to avoid writing low-level HTTP RESTful GET calls to authenticate users. The login UI comes up automatically and the access token is passed back from the sign-in control for future utilization.

<live:SignInButton ClientId="00000000400BDB2B" Scopes="wl.basic wl.offline_access wl.signin wl.contacts_birthday" Branding="Windows" TextType="Login"  d:LayoutOverrides="VerticalAlignment" SessionChanged="SignInButton_SessionChanged" />

Our app may need to access different types of user’s information. To do that the app needs to get permission from user which is defined in Scopes parameter. In this article we will get user’s basic information like name, profile picture and birthday. When user clicks on the SignInButton the event handler SignInButton_SessionChanged() is called which generates a session and calls the LoginIn() method which finally calls the GetUserProfile() to get the user’s profile information. In GetUserProfile() we get the user’s basic profile information and user’s profile image.

private void GetUserProfile()
{
LiveConnectClient clientGetMe = new LiveConnectClient(Session);
clientGetMe.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(clientGetMe_GetCompleted);
clientGetMe.GetAsync("me", null);
 
LiveConnectClient clientGetPicture = new LiveConnectClient(Session);
clientGetPicture.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(clientGetPicture_GetCompleted);
clientGetPicture.GetAsync("me/picture");
}

We parse the user name, profile image and user id. The clientGetPicture_GetCompleted() method parse the user’s image and displays it on the screen.

void clientGetPicture_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
string image = (string)e.Result["location"];
imageUser.Source = new BitmapImage(new Uri(image, UriKind.RelativeOrAbsolute));
}
}

The clientGetMe_GetCompleted() method parse user’s name and user id.

void clientGetMe_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
textBlockName.Text = (string)e.Result["name"];
string uid = (string)e.Result["id"];
LiveConnectClient readBday = new LiveConnectClient(Session);
readBday.GetCompleted +=
new EventHandler<LiveOperationCompletedEventArgs>(ReadBday_GetCompleted);
readBday.GetAsync(uid);
}
}

We use the user id to get user’s birthday information in ReadBday_GetCompleted() method.

void ReadBday_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
string bday = e.Result["birth_day"].ToString();
string bmonth = e.Result["birth_month"].ToString();
string bMonthString="";
 
//convert month
switch (bmonth)
{
case "1":
bMonthString = "January";
break;
case "2":
bMonthString = "February";
break;
case "3":
bMonthString = "March";
break;
case "4":
bMonthString = "April";
break;
case "5":
bMonthString = "May";
break;
case "6":
bMonthString = "June";
break;
case "7":
bMonthString = "July";
break;
case "8":
bMonthString = "August";
break;
case "9":
bMonthString = "September";
break;
case "10":
bMonthString = "October";
break;
case "11":
bMonthString = "November";
break;
case "12":
bMonthString = "December";
break;
default:
break;
}
// assign the textblock
textBlockBday.Text = bday + "," + " " + bMonthString;
textBlockHeader.Visibility = Visibility.Visible;
textBlockBtitle.Visibility = Visibility.Visible;
}
}

Summary

The Live Connect APIs use standard protocols such as HTTP, OAuth 2.0, JSON and XMPP to make it easy working with them across multiple platforms. To leverage the APIs, we primarily use Representational State Transfer (REST) requests which return information in JSON. In this article we focused on how to use the Live SDK to get information on user’s Windows Live ID, but this Live SDK can also be used to get information about SkyDrive, Hotmail and Messenger also. To know more about Live Connect APIs and services please visit Live Connect Developer Center.

There is also a video on Live Connect API and SkyDrive implementation.

Source Code

This page was last modified on 15 July 2013, at 11:11.
357 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×