Getting user data (Android)

The Live SDK for Android enables your Android app to access a user's info on Microsoft OneDrive.

In this article
Prerequisites
Step 1: Requesting info
Step 2: Requesting info about the user's friends
Step 3: Working with info

To access a user's info from Microsoft OneDrive, 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.

Prerequisites

This topic assumes that your app has initialized the Live SDK 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.

Step 1: 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.

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() {
    ...
});
...

Step 2: Requesting info about the user's friends

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() {
    ...
});
...

Step 3: Working with info

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());
		}
	});
}

See also

Other resources

Android reference