Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This is the third tutorial in the tutorial series that guides you on calling a protected web API using Microsoft Entra External ID.
In this tutorial, you:
- Call a protected web API
In app > src > main> java > com.example(your app name). Create the following Android fragments:
- MSGraphRequestWrapper
Open MSGraphRequestWrapper.java and replace the code with following code snippet to call the Microsoft Graph API using the token provided by MSAL:
package com.azuresamples.msalandroidapp; import android.content.Context; import android.util.Log; import androidx.annotation.NonNull; import com.android.volley.DefaultRetryPolicy; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; public class MSGraphRequestWrapper { private static final String TAG = MSGraphRequestWrapper.class.getSimpleName(); // See: https://docs.microsoft.com/en-us/graph/deployments#microsoft-graph-and-graph-explorer-service-root-endpoints public static final String MS_GRAPH_ROOT_ENDPOINT = "https://microsoftgraph.chinacloudapi.cn/"; /** * Use Volley to make an HTTP request with * 1) a given MSGraph resource URL * 2) an access token * to obtain MSGraph data. **/ public static void callGraphAPIUsingVolley(@NonNull final Context context, @NonNull final String graphResourceUrl, @NonNull final String accessToken, @NonNull final Response.Listener<JSONObject> responseListener, @NonNull final Response.ErrorListener errorListener) { Log.d(TAG, "Starting volley request to graph"); /* Make sure we have a token to send to graph */ if (accessToken == null || accessToken.length() == 0) { return; } RequestQueue queue = Volley.newRequestQueue(context); JSONObject parameters = new JSONObject(); try { parameters.put("key", "value"); } catch (Exception e) { Log.d(TAG, "Failed to put parameters: " + e.toString()); } JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, graphResourceUrl, parameters, responseListener, errorListener) { @Override public Map<String, String> getHeaders() { Map<String, String> headers = new HashMap<>(); headers.put("Authorization", "Bearer " + accessToken); return headers; } }; Log.d(TAG, "Adding HTTP GET to Queue, Request: " + request.toString()); request.setRetryPolicy(new DefaultRetryPolicy( 3000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); queue.add(request); } }
Build and deploy the app to a test device or emulator. You should be able to sign in and get tokens for Microsoft Entra ID.