Export (0) Print
Expand All

Streamlining account registration

This topic describes how your app can get a user's consent to access his or her info—specifically, the user's first name, last name, email address, gender, and birthday.

Most users don't like to enter their personal info in multiple apps, especially if it requires them to create an app-specific account. If your app needs to create an account for a user, you can use the Live Connect APIs to streamline your account-registration process by using the info that the users have already stored in their Microsoft account—for example, an email address, which your app can access by using the Live Connect APIs. This approach can make creating an app-specific account as simple for the user as clicking a link and typing his or her Microsoft account credentials.

The examples in this topic illustrate how to get users' info from their Microsoft account by using the Live Connect APIs. The code examples here assume that your app has initialized the APIs, that the user has signed in, and that he or she has consented to these scopes:

  • wl.basic, which allows your app to get the user's name and gender
  • wl.emails, which allows your app to get the user's email address
  • wl.birthday, which allows your app to get the user's birthday info

For more info about these initial steps, see Signing users in and Obtaining user consent.

In these examples, the GET calls request info about me, which is a reserved word that returns info from the signed-in user's profile. After the Live Connect APIs return the data to your app, your app can insert the user's info into UI controls. This is demonstrated in some of these examples.

To make full use of the code examples in this topic, you can use them in the context of larger code reference samples that we provide in Working with the code examples.

Getting and working with user info in JavaScript and HTML for Windows Store apps

In this example, response.structure_name is used to get the info in the corresponding structure. For example, response.first_name returns the user's first name. Also, note that you can access response.emails.preferred to get info from the preferred structure.


function streamlineAccountReg_onClick() {
    WL.login({
        scope: ["wl.signin", "wl.basic", "wl.birthday", "wl.emails"]
    }).then(
        function (response) {
            WL.api({
                path: "me",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("first_name").innerText = response.first_name;
                    document.getElementById("last_name").innerText = response.last_name;
                    document.getElementById("email").innerText = response.emails.preferred;
                    document.getElementById("gender").innerText = response.gender;
                    document.getElementById("birthday").innerText =
                        response.birth_month + " " + response.birth_day + " " + response.birth_year;
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        }, 
        function (responseFailed)
        {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}


Getting and working with user info in C# for Windows Store apps and Windows Phone apps


private async void btnFillRegistration_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult = await liveClient.GetAsync("me");
        dynamic result = operationResult.Result;
        firstNameTextBlock.Text = result.first_name;
        lastNameTextBlock.Text = result.last_name;
        emailTextBlock.Text = result.emails.preferred;
        genderTextBlock.Text = result.gender;
        birthdayTextBlock.Text = string.Join("/", result.birth_month, result.birth_day, result.birth_year);
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting user info: " + exception.Message;
    }
}


Getting and working with user info in Objective-C for iOS


-(void) streamlineAccountRegistration
{
    [self.liveClient getWithPath:@"me" 
                        delegate:self];
}

- (void) liveOperationSucceeded: (LiveOperation *)operation
{
    NSDictionary *emails = [operation.result objectForKey:@"emails"];
    NSString *preferred = [emails objectForKey:@"preferred"];
    NSString *birth_month = [operation.result objectForKey:@"birth_month"];
    NSString *birth_day = [operation.result objectForKey:@"birth_day"];
    NSString *birth_year = [operation.result objectForKey:@"birth_year"];
    [self.firstNameLabel setText:[NSString stringWithFormat: @"%@", [operation.result objectForKey:@"first_name"]]];
    [self.lastNameLabel setText:[NSString stringWithFormat: @"%@", [operation.result objectForKey:@"last_name"]]];
    [self.emailLabel setText:[NSString stringWithFormat: @"%@", preferred]];
    [self.genderLabel setText:[NSString stringWithFormat: @"%@", [operation.result objectForKey:@"gender"]]];
    [self.birthdayLabel setText:[NSString stringWithFormat: @"%@ %@ %@", birth_day, birth_month, birth_year]];
}


Getting and working with user info in Java for Android


public void streamlineAccountRegistration() {
    client.getAsync("me", new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
            resultTextView.setText("Error getting user info: " + exception.getMessage());
        }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            fillUserForm(result);
        }
    });
}

private void fillUserForm(JSONObject user) {
    String firstName = user.optString("first_name");
    String lastName = user.optString("last_name");
    JSONObject emails = user.optJSONObject("emails");
    String email = "";
       if (emails != null) {
        email = emails.optString("preferred");
       }
    resultTextView.setText("First name = " + firstName + "\n" +
        "Last name = " + lastName + "\n" +
        "Preferred email = " + email);
}


Getting and working with user info using REST

After you make the appropriate GET call in Representational State Transfer (REST), the user's info is in the response. Here's how to make a GET call. (In this example, the signed-in user is represented as me and ACCESS_TOKEN is replaced by the actual access token string that was returned when the user signed in.)

GET https://apis.live.net/v5.0/me?access_token=ACCESS_TOKEN

The Live Connect APIs return data to your app as a JavaScript Object Notation (JSON) object, which contains structures that correspond to the scopes that you requested for your app.

Here's an example of a JSON object returned by a REST call to a Live Connect API.

{
    "first_name" : "Luka",
    "last_name" : "Abrus",
    "emails : {
        "preferred": "luka.abrus@fabrikam.com"
    },
    "birth_month" : 6,
    "birth_date" : 26,
    "birth_year" : 1973
}

Next steps

This topic shows how to streamline app-specific account registration by using the Live Connect APIs. Learn more about:

Top

 

 

Show:
© 2014 Microsoft