Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
NakulSabharwal edited this page Mar 13, 2019 · 2 revisions

Welcome to the msgraph-sdk-android-auth wiki!

Sample

This Sample shows how to authenticate using this library get your messages

Install msgraph-sdk-android-auth in your app

In project level build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        jcenter{  // add this repository
            url 'http://oss.jfrog.org/oss-snapshot-local'
        }
    }
}

In app level build.gradle

dependencies {
    ...
    implementation 'com.microsoft.graph:microsoft-graph-android-auth:0.1.0-SNAPSHOT'
}

AndroidManifest.xml

Add this activity

<activity
     android:name="com.microsoft.identity.client.BrowserTabActivity">
     <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE" />
     <data android:scheme="msal<YOUR_CLIENT_ID>"
            android:host="auth" />
     </intent-filter>
</activity>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    List<String> messages = null;
    ArrayAdapter adapter =null;
    MSALAuthenticationProvider msalAuthenticationProvider;
    final static String SCOPES [] = "https://graph.microsoft.com/User.Read",
                                    "https://graph.microsoft.com/Mail.ReadWrite"};
    IGraphServiceClient graphClient;
    PublicClientApplication publicClientApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        messages = new ArrayList<>();
        adapter =   new ArrayAdapter<String>(this,R.layout.listview, messages);

        ListView listView = (ListView) findViewById(R.id.message_list);
        listView.setAdapter(adapter);

        // Initialize PublicClientApplication instance
        publicClientApplication = new PublicClientApplication(getApplicationContext(), "CLIENT_ID");
        
        // Initialize MSALAuthenticationProvider instance
        msalAuthenticationProvider = new MSALAuthenticationProvider(this,
                getApplication(),
                publicClientApplication,
                SCOPES);

        // Create a GraphClient Instance
        graphClient = GraphServiceClient
                        .builder()
                        .authenticationProvider(msalAuthenticationProvider)
                        .buildClient();

        // Use GraphClient instance to make requests
        graphClient.me().messages().buildRequest().get(new ICallback<IMessageCollectionPage>() {
            @Override
            public void success(IMessageCollectionPage iMessageCollectionPage) {
                // Successful response, use it in your application
                // using first page of messages to update UI
                for(Message message : iMessageCollectionPage.getCurrentPage()){
                    messages.add(message.subject);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void failure(ClientException ex) {
                
            }
        });
    }

    // Add this function in your Authenticating activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        msalAuthenticationProvider.handleInteractiveRequestRedirect(requestCode, resultCode, data);
    }
}