Steps I have taken:
1. Added users and created groups in Azure
2. In Visual Studio I have created a web api which automatically creates an app registration in Azure.
3. In Visual Studio web api I have created an App service which automatically shows in Azure.
4. In Visual Studio web api I have created a connection to Azure Active directory by passing in the "AppId Uri" which you get from the app registration.
5. In Azure portal, in the app service I have turn on and set up advanced settings for Azure active directory.
6. In Azure portal, in app service I have given roles to users and groups in the "Access Control"
7. In Azure go to App Registration and build the app in android.
8. Open the app in Android studio and it allows you to log in and log out with the Active directory.
9. In the Android app once you have logged in, active directory returns an access token. I am passing the access token into the http request header to access the api which needs authentication, this gives me a 401.
here is my http request:
RequestQueue queue = Volley.newRequestQueue(this);
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, "https://garethtesterwebapi20190618012925.azurewebsites.net/api/values",
parameters,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
/* Successfully called graph, process data and send to UI */
Log.d(TAG, "Response: " + response.toString());
updateGraphUI(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.networkResponse.statusCode);
Log.d(TAG, "Error: " + error.networkResponse.allHeaders);
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
//headers.put("Content-Type", "application/json; charset=UTF-8");
headers.put("Authorization", "Bearer " + authResult.getAccessToken());
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);
Can anyone tell me why I am getting a 401 and how to solve this??
Any help would be greatly apprenticed.
Thanks
Gareth