Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Determining a user's age

This topic describes and demonstrates how to get a user's age, if he or she chose to store this info in Live services. The example provided here disables a purchase control until the app can verify that the signed-in user is at least 18 years old.

When your app interacts with users, some info is critical to ensuring the right user experience. Knowing the user's age is a great example, especially if your app allows actions such as in-app purchases of age-restricted media.

This topic assumes that your app has already initialized the Live Connect APIs, that the user has signed in, and that he or she has consented to the wl.birthday scope (which grants access to the user's birthday info). For details about these initial steps, see Signing users in and Obtaining user consent.

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.

Working with the user's age in JavaScript and HTML for Windows Store apps

In this code example, the WL.api function requests info for me, which returns the signed-in user's profile info.

After WL.api returns, response.birth_year contains the birth year that the user stored in the profile, which the code uses to compute the user's age.


function userAge_onClick() {
    WL.login({
        scope: ["wl.signin", "wl.birthday"]
    }).then(
        function (response) {
            WL.api({
                path: "me",
                method: "GET"
            }).then(
                function (response) {
                    if (response.birth_year) {
                        var date = new Date();
                        var year = date.getFullYear();
                        var age = year - response.birth_year;
                        if (age >= 18) {
                            document.getElementById("purchaseButton").disabled = "";
                            document.getElementById("infoArea").innerText =
                                "Age is 18 or older. Purchase button enabled.";
                        }
                        else {
                            document.getElementById("purchaseButton").disabled = "disabled";
                            document.getElementById("infoArea").innerText =
                                "Age is under 18. Purchase button disabled.";
                        }
                    }
                    else {
                        document.getElementById("purchaseButton").disabled = "disabled";
                        document.getElementById("infoArea").innerText =
                            "Cannot determine age. Purchase button disabled.";
                    }
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}

function purchaseButton_onClick() {
    document.getElementById("infoArea").innerText =
        "Purchase button clicked."; 
}


Top

Working with the user's age in C# for Windows Store apps and Windows Phone apps

In this code example, the LiveConnectClient.GetAsync method requests info for me, which returns the signed-in user's profile info. The user's age is computed from the birth year in the returned info.


private async void btnAge_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult = await liveClient.GetAsync("me");
        dynamic result = operationResult.Result;
        if (result.birth_year != null)
        {
            DateTime date = DateTime.Now;
            int year = date.Year;
            int age = year - (int)result.birth_year;
            if (age >= 18)
            {
                infoTextBlock.Text = "Age is 18 or older. Purchase button enabled.";
                btnPurchase.IsEnabled = true;
            }
            else
            {
                infoTextBlock.Text = "Age is under 18. Purchase button disabled.";
            }
        }
        else
        {
            infoTextBlock.Text = "Could not determine birth year. Purchase button disabled.";
        }

    }
    catch (LiveConnectException exception)
    {
        infoTextBlock.Text = "Error getting user info: " + exception.Message;
    }
}

private void btnPurchase_Click(object sender, RoutedEventArgs e)
{
    infoTextBlock.Text = "Purchase button clicked.";
}


Top

Working with the user's age in Objective-C for iOS

In this code example, the LiveConnectClient getWithPath:delegate method requests info for me, which returns the signed-in user's profile info. The liveOperationSucceeded:operation method then computes the user's age from the birth year in the returned info to determine whether the purchase button should be enabled.


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

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSDate *currentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* dateComponents = [calendar components:NSYearCalendarUnit 
                                                   fromDate:currentDate];
    NSInteger currentYear = [dateComponents year];
    NSInteger usersBirthYear = (int)[operation.result objectForKey:@"birth_year"];
    NSInteger age = currentYear - usersBirthYear; 
    if (age >= 18)
    {
        self.purchaseButton.enabled = YES;
    }
    else
    {
        self.purchaseButton.enabled = NO;
    }
}


Top

Working with the user's age in Java for Android

In this code example, the LiveConnectClient.getAsync method requests info for me, which returns the signed-in user's profile info. The custom updateRestrictedButton method then computes the user's age from the birth year in the returned info to determine whether the purchase button should be enabled.


public void getUserAge() {
    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();
            updateRestrictedButton(result);
        }
    });
}

private void updateRestrictedButton(JSONObject user) {
    boolean enable = false;
    if (user != null) {
        int birthYear = user.optInt("birth_year");
        if (birthYear > 0) {
            int year = new Date().getYear();
            int age = year - birthYear;
            if (age >= 18) {
                enable = true;
            }
        }
    }
    if (enable == true) {
        resultTextView.setText("Purchase button enabled.");
    }
    else {
        resultTextView.setText("Purchase button disabled.");
    }
}


Top

Working with the user's age using REST

After you make the appropriate GET call in Representational State Transfer (REST), the user's info is in the response. The response is returned in JavaScript Object Notation (JSON) format, which you can parse to get the individual structures within. Here's how to make a GET call where the signed-in user is represented as me.

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

Here's what the JSON-formatted info that is returned might look like for a fictional user.

{
  "first_name" : "Luka",
  "last_name" : "Abrus",
  "birth_month" : 6,
  "birth_date" : 26,
  "birth_year" : 1968
}

Top

Next steps

This topic demonstrates how to use the wl.birthday scope to get a user's birth year and determine whether the user is old enough to make a certain type of purchase through your app. Learn more about:

Top

 

 

Show:
© 2015 Microsoft