Windows apps
Collapse the table of content
Expand the table of content
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.

Getting user data

To access a user's info from Outlook.com, Microsoft OneDrive, and other services that are compatible with Live Connect, your app must check for two things. First, it must confirm that the user has signed in successfully. Second, it must request and receive the user's consent, to work with his or her info. For details about these steps, see Signing users in and Obtaining user consent.

This topic assumes that your app has initialized the Live Connect APIs successfully, the user has signed in successfully, and the user has consented to the wl.basic scope, which grants access to basic info about the user and his or her contacts.

To fully understand 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.

Requesting info

There are two different ways to work with a user's info.

  • The user ID of a person or contact.
  • The me shortcut that provides access to the signed-in user.

These code examples show how to use these different methods in the languages and platforms that the Live Connect APIs supports.

Requesting info in JavaScript and HTML for Windows Store apps

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info.

function readContact() {
    WL.api({
        path: "USER_ID",
        method: "GET"
    }, onReadContactCallback);
}

function onReadContactCallback(response) {
    if (!response.error) {
        var name = response.first_name;
        // Display user's first name.
    }
}

This code example shows how to use the me shortcut to request info about the signed-in user.


...
WL.api({
    path: "me",
    method: "GET"
}, onApiCalled);
... 

This code example shows how to use the me shortcut to get a list of the signed-in user's friends.


...
WL.api({
    path: "me/friends",
    method: "GET"
}, onApiCalled);
... 

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the string value shown for the path parameter was in the collection returned by the preceding example. In your code, you would use a value returned by the preceding call and not a string constant as shown in this example.

...
WL.api({
    path: "contact.c1678ab4000000000000000000000000",
    method: "GET"
}, onApiCalled);
... 

To work with info, use the WL.api method. For example, here's how to make a GET call to get the first name of a user's friend.


function getFriendName_onClick() {
    WL.login({
        scope: "wl.basic"
    }).then(
        function (response)
        {
            WL.api({
                path: "contact.09fb5b27000000000000000000000000",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText =
                        "Friend's name is: " + response.first_name;
                }, 
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}


Requesting info in C# for Windows Store apps and Windows Phone apps

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info.

private async void readContact(object sender, RoutedEventArgs e)
{
    try
    {
        LiveOperationResult operationResult = await client.GetAsync("USER_ID");
        dynamic result = operationResult.Result;
        string name = result.first_name;
        // Display user's first name.
    }
    catch (LiveConnectException exception)
    {
        // Display error if operation is unsuccessful.
    }
}

This code example shows how to use the me shortcut to request info about the signed-in user.

...
LiveOperationResult operationResult = await client.GetAsync("me");
...

This code example shows how to use the me shortcut to get a list of the signed-in user's friends.

...
LiveOperationResult operationResult = await client.GetAsync("me/friends");
...

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the string value shown as the parameter to GetAsync was in the collection returned by the preceding example. In your code, you would use a value returned by the preceding call and not a string constant as shown in this example.

...
LiveOperationResult operationResult = await client.GetAsync("contact.c1678ab4000000000000000000000000");
...

To work with info, use methods of the LiveConnectClient class, such as GetAsync, PostAsync, PutAsync, and DeleteAsync for GET, POST, PUT, and DELETE calls, respectively. For example, here's how to get the first name of a user's friend.


private async void btnFriendName_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveAuthClient auth = new LiveAuthClient();
        LiveLoginResult loginResult = await auth.LoginAsync(new string[] { "wl.basic" });
        if (loginResult.Status == LiveConnectSessionStatus.Connected)
        {
            this.infoTextBlock.Text = "Signed in.";
        }
        LiveConnectClient connect = new LiveConnectClient(auth.Session);
        LiveOperationResult operationResult = await connect.GetAsync("me/friends");
        dynamic resultList = operationResult.Result;
        StringBuilder friendsNames = new StringBuilder();
        foreach (dynamic result in resultList.data)
        {
            friendsNames.Append(result.name);
            friendsNames.Append("\n");
        }
        this.infoTextBlock.Text = friendsNames.ToString();
    }
    catch (LiveAuthException exception)
    {
        this.infoTextBlock.Text = "Error signing in: " + exception.Message;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting friend(s) name(s): " + exception.Message;
    }
}


Requesting info in Objective-C for iOS

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info.

- (IBAction)getMe:(id)sender
{
    [self.liveClient getWithPath:@"USER_ID"
                        delegate:self
                       userState:@"getUserID"];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    if ([operation.UserState isEqual:@"getUserID"]) {
        [self.infoLabel setText:[NSString stringWithFormat:@"@", [operation.result objectForKey:@"first_name"]]];
    }
}

- (void) liveOperationFailed:(NSError *)error operation:(LiveOperation *)operation
{
    if ([operation.userState isEqual:@"getUserID"]) {
        [self.infoLabel setText:[NSString stringWithFormat:@"%@: %@", @"Error getting name", [error localizedDescription]]];
    }
}

This code example shows how to use the me shortcut to request info about the signed-in user.

...
[self.liveClient getWithPath:@"me"
                    delegate:self
                   userState:@"getMe"];
...

This code example shows how to use the me shortcut to get a list of the signed-in user's friends.


...
[self.liveClient getWithPath:@"me/friends"
                    delegate:self
                   userState:@"getFriends"];
...

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the string value shown as the parameter to getWithPath was in the collection returned by the preceding example. In your code, you would use a value returned by the preceding call and not a string constant as shown in this example.

...
[self.liveClient getWithPath:@"contact.c1678ab4000000000000000000000000"
                    delegate:self
                   userState:@"getContact"];
...

To work with info, use methods of the LiveConnectClient interface, such as getWithPath, postWithPath, putWithPath, and deleteWithPath for GET, POST, PUT, and DELETE calls, respectively. For example, here's how to get the first name of a user's friend.


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

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSArray *contacts = [operation.result objectForKey:@"data"];
    NSDictionary *firstContact = [contacts objectAtIndex:0];
    self.displayLabel.text = [firstContact objectForKey:@"first_name"];
}


Requesting info in Java for Android

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info.


...
this.greetUserButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        client.getAsync("USER ID", new LiveOperationListener() {
            public void onComplete(LiveOperation operation) {
                try {
                    JSONObject result = operation.getResult();
                    String name = result.getString("first_name");
                    // Display user's first name.
                } catch (JSONException e) {
                    // Display error if first name is unavailable.
                    return;
                }
            }
            public void onError(LiveOperationException exception, LiveOperation operation) {
                // Display error if operation is unsuccessful.
            }
        });
    }
});
...

This code example shows how to use the me shortcut to request info about the signed-in user.


...
client.getAsync("me", new LiveOperationListener() {
    ...
});
...

This code example shows how to use the me shortcut to get a list of the signed-in user's friends.

...
client.getAsync("me/friends", new LiveOperationListener() {
    ...
});
...

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the string value shown as the parameter to getAsync was in the collection returned by the preceding example. In your code, you would use a value returned by the preceding call and not a string constant as shown in this example.

...
client.getAsync("contact.c1678ab4000000000000000000000000", new LiveOperationListener() {
    ...
});
...

To work with info, use methods of the LiveConnectClient class, such as getAsync, postAsync, putAsync, and deleteAsync for GET, POST, PUT, and DELETE calls, respectively. For example, here's how to get the first name of a user's friend.


public void getFriendName() {
    client.getAsync("contact.c1678ab4000000000000000000000000", new LiveOperationListener() {
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            resultTextView.setText("Friend's name = " + result.optString("first_name"));
        }
        public void onError(LiveOperationException exception, LiveOperation operation) {
            resultTextView.setText("Error getting friend's name: " + exception.getMessage());
        }
    });
}


Requesting info using REST

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info and ACCESS_TOKEN with the access token provided when the user signed in.

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

This code example shows how to use the me shortcut to request info about the signed-in user. In your code, replace ACCESS_TOKEN with the access token provided when the user signed in.

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

This code example shows how to use the me shortcut to get a list of the signed-in user's friends. In your code, replace ACCESS_TOKEN with the access token provided when the user signed in.

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

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the contact string shown was in the response returned by the preceding example. In your code, you would use a value returned by the preceding call and not the contact string shown in this example. Also, in your code, replace ACCESS_TOKEN with the access token provided when the user signed in.

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

In REST, after you make the appropriate GET call, the first name of the user's friend's is in the response. Here's how to make a call by using the friend's ID. In this example, the contact string shown was in the response returned by the example above. In your code, you would use a value returned in your app and not the contact string shown in this example. Also, in your code, replace ACCESS_TOKEN with the access token provided when the user signed in.

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

Top

Working with info

The Live Connect APIs return the response as info formatted in JavaScript Object Notation (JSON). Here's a JSON-formatted object that contains a user's publicly available info.

{
  "id" : "contact.c1678ab4000000000000000000000000",
  "first_name" : "Roberto",
  "last_name" : "Tamburello",
  "name" : "Roberto Tamburello",
  "gender" : "male",
  "locale" : "en_US"
} 

The REST reference topic describes the JSON-formatted object that is returned in response to each type of request.

Top

Next steps

Now you know how to get info about a user by using the Live Connect APIs. Learn more about:

Top

 

 

Show:
© 2017 Microsoft