404 file not found error when using microsoft grap

2019-07-31 01:30发布

问题:

I am getting following error when calling the graph api, https://graph.microsoft.com/v1.0/me/drive/root:/foldername using the access token got through open id connect protocol, but if i use same token to get to https://graph.microsoft.com/v1.0/me API it works, and the folder which i am searching is really exist on one drive for buisness but still i am getting this error, when i run same code after some time, i found that it works fine. what could be the issue. even for this api https://graph.microsoft.com/v1.0/me/drive/root/children i get following error.

    {
  "error": {
    "code": "UnknownError",
    "message": "404 FILE NOT FOUND",
    "innerError": {
      "request-id": "d7cdf434-ead1-445d-b2be-f269a82027eb",
      "date": "2016-07-07T03:38:47"
    }
  }
}

回答1:

In case you end up here using app-only authorization:

As mentioned in this answer App-only authorization is currently not supported for accessing OneDrive for Business through Microsoft Graph API and will throw 404 FILE NOT FOUND exceptions.

To solve this I used the password grant_type flow to request the token:

POST https://login.microsoftonline.com/{tenantId}.onmicrosoft.com/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=password
&username=<username>
&password=<password>
&client_id=<clientId>
&client_secret=<clientSecret>
&resource=https://graph.microsoft.com


回答2:

This "404 FILE NOT FOUND" error occurs when the user doesn't have Office 365 / OneDrive or has it but hasn't initialized OneDrive (i.e. navigated to https://[domain]-my.sharepoint.com/personal/).

Once the user gets O365/OneDrive and initializes it, you should be able to successfully query https://graph.microsoft.com/v1.0/me/drive/root/children.

You should also be able to query https://graph.microsoft.com/v1.0/me/drive/root:/foldername

If you indeed you were to come across a legitimate case of folder not found, you'd get this error message:

{
    "error": {
        "code": "itemNotFound",
        "message": "The resource could not be found.",
        "innerError": {
            "request-id": "1ce2bd8d-a88f-42ae-affd-ee90f3c1d4fb",
            "date": "2016-07-11T03:17:25"
        }
    }
}