Search code examples
javamicrosoft-graph-apionedrive

Microsoft Graph on android, responds 404 when retrieving file


On microsoft graph explorer i am able to retrieve the excel file easily but when trying on the "active-directory-android-native-v2-master" sample code, it returns 404.

On the the other hand, next line works and retrieves my information correctly

final static String MSGRAPH_URL = "https://graph.microsoft.com/v1.0/me";

I added all required permissions, got the client ID, run all the sample instructions, read the documentation + stack over flow.

I thought it might be because the link was not coded correctly so i modified the callGraphAPI() method to include:

Uri.Builder builder = new Uri.Builder();
builder.
        scheme("https").
        authority("graph.microsoft.com").
        appendPath("v1.0").
        appendPath("drives").
        appendPath(MY_DRIVE).
        appendPath("items").
        appendPath(FILE_ID).
        appendPath("workbook");
String url = builder.build().toString();

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,url/*MSGRAPH_URL*/,
        parameters,new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {

but still the same response.

I came across the next stack overflow answer

404 file not found error when using microsoft graph api

And thought it might be the answer but then seen the question was old and another answer mentioned it is not longer correct.

appreciate any help.


Solution

  • It seems i was not aware using sample code for the V2 (active-directory-android-native-v2-master) while the graph explorer (which was working) used V1.

    There is a great "getting started" tutorial for the V1 sample code (active-directory-android-master) here:

    https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-v1-android

    Sample code seems quite the same only for V1.

    When using the tutorial, i needed to find the required permissions for the command i was trying to use, in addition to the one mentioned at the tutorial.

    I used the next link to get the permissions for the items:

    https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/shares_get

    I also added the permissions mentioned at the Graph Exlorer since as noted, the command i was trying to use worked there.

    Since the permissions at the Azure site was not written the same (e.g. Files.Read is written as "Read user files") i used the next link to translate:

    https://developer.microsoft.com/en-us/graph/docs/concepts/permissions_reference

    Luckily i did not need to use any admin permissions, which would have complicated the registration to the app

    Hopes this helps any struggled newbie like me :)

    If anything from what i wrote not correct or you think i should add something, please let me know at the comments below and i will try to update

    BTW - i used the Graph Exlorer to detect the files & Drive ID items i needed