Signing users in to OneDrive (Android)

Let users sign in to their Microsoft OneDrive from your Android apps.

In this article
Prerequisites
Register your app and configure its settings
Specify a redirect domain and get a client secret
Specify consent page settings
Compile the Android API source code
Reference the compiled Android API source code in an Eclipse Android project
Sign the user in
Summary

The user must be signed in to his or her Microsoft account for your app to work with the user's info on OneDrive. You help the user do this by adding sign-in functionality to your app in the form of a button or a hyperlink.

Prerequisites

We assume that you know how to write Android apps. You also need the following:

  • You must download and install the Live SDK for Android.

  • You must have an app to which you want to add sign-in functionality.

Register your app and configure its settings

  1. Go to the Microsoft account Developer Center.

  2. If prompted, sign in with your Microsoft account credentials.

  3. If you are not immediately prompted to provide the app's display name and primary language, click Create application.

  4. Type the app's display name and select the app's primary language.

  5. Read the Live Connect terms of use and the Privacy and Cookies statement, and then click I accept. A client ID is created and displayed in App Settings. It should look something like this: 00000000603E0BFE.

  6. To specify that your app is a mobile client app, in API Settings, click Yes under Mobile or desktop client app.

Specify a redirect domain and get a client secret

  1. In the Microsoft account Developer Center, select your app and click Edit settings > API Settings.

  2. Under Redirect URLs, type the redirect domain you will be redirecting users to

  3. Click App Settings. On the application summary page, the client secret is displayed. It should look something like this:

    qXipuPomaauItsIsmwtKZ2YacGZtCyXD

    Note

    To change your client secret, click App Settings > Create a new client secret > OK. Both the old and new client secrets will continue to work until you activate the new client secret from API Settings in the Microsoft account Developer Center.

After users sign in from your app to a service that is compatible with Microsoft account, the Allow access page (also called the consent page) is displayed, which explains to the user the types of info that your app wants to access. To customize the items in the consent page, go to your Microsoft account Developer Center and specify the following items.

Consent page

App management site setting

Your app's logo

Basic Information page, Application logo image

Your app's domain name, which appears in several places in the consent page

API Settings page, Root Domain box

Your app's name, which appears in several places in the consent page

Basic Information page, Application name box

Your app's terms-of-use hyperlink

Basic Information page, Terms of service URL box

Your app's privacy statement hyperlink

Basic Information page, Privacy URL box

Note

If the Terms of service URL and Privacy URL settings are not specified, the text | APPLICATION_NAMEterms and privacy statement does not appear in the consent page.

Compile the Android API source code

You need to compile the Android API source code only once. After you compile it on your computer, you can reference it from multiple Eclipse Android projects on that same computer.

  1. Start Eclipse, if it's not already running.

  2. Click File > Import.

  3. Expand General, click Existing Projects into Workspace, and then clickNext.

  4. With the Select root directory option selected, click Browse.

  5. Go to and select the src folder within the downloaded Live SDK for Android, and then click OK.

  6. In the Projects box, select the LiveSdk check box.

  7. With the Copy projects into workspace check box selected, click Finish. Eclipse adds the LiveSdk project to the Package Explorer pane and then compiles the Android API source code in the background.

You can now reference the compiled Android API source code from your Eclipse Android projects.

Reference the compiled Android API source code in an Eclipse Android project

  1. In Eclipse, display the Package Explorer pane, if it's not already visible.

  2. Right-click your project's name, and then click Properties.

  3. In the list of project properties, click Android.

  4. In the Library area, click Add.

  5. Click LiveSdk and then click OK.

  6. Click OK. Eclipse sets a reference to the compiled Android API source code project, and you can now call the Live SDK Android API from your own Android project.

Before you run your project, you must add the Internet permission to your project's manifest, as shown in the following steps. If you don't add the Internet permission, your app may have problems accessing OneDrive web services later.

  1. With your project open, in the Package Explorer pane, open the AndroidManifest.xml file.

  2. In the editor, click the Permissions tab.

  3. Click Add.

  4. Click Uses Permission and then click OK.

  5. In the Name list, click android.permission.INTERNET.

  6. Save the AndroidManifest.xml file.

Sign the user in

Here's the basic code that shows how to reference the Android API, invite a user to sign in, and get the signed-in user's permission to get his or her basic profile info.

mainpage.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Results will display here." />
</LinearLayout>

JavaCodeSample.java

package com.live.dev;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Arrays;
import com.microsoft.live.LiveAuthException;
import com.microsoft.live.LiveAuthListener;
import com.microsoft.live.LiveAuthClient;
import com.microsoft.live.LiveConnectSession;
import com.microsoft.live.LiveConnectClient;
import com.microsoft.live.LiveStatus;

public class JavaCodeSample extends Activity implements LiveAuthListener {
        
    private LiveAuthClient auth;
    private LiveConnectClient client;
    private TextView resultTextView;    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.resultTextView = (TextView)findViewById(R.id.resultTextView);
        this.auth = new LiveAuthClient(this, MyConstants.APP_CLIENT_ID);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Iterable<String> scopes = Arrays.asList("wl.signin", "wl.basic");
        this.auth.login(scopes, this);
    }
    
    public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
        if(status == LiveStatus.CONNECTED) {
            this.resultTextView.setText("Signed in.");
            client = new LiveConnectClient(session);
        }
        else {
            this.resultTextView.setText("Not signed in.");
            client = null;
        }        
    }

    public void onAuthError(LiveAuthException exception, Object userState) {
        this.resultTextView.setText("Error signing in: " + exception.getMessage());        
        client = null;        
    }
}

MyConstants.java (where MY CLIENT ID is your app's client ID for example, 00000000603E7410)

package com.live.dev;

final class MyConstants {
    static final String APP_CLIENT_ID = "MY CLIENT ID";
}

Summary

You have just seen how to sign your users in to their OneDrive account. Next, you might want to give them the ability to work with their OneDrive files and folders in your app. To do this, see File and folder properties.