Search code examples
office365microsoft-graph-apionedrive

Bad Request - personal onedrive account - https://graph.microsoft.com/v1.0/me/drive/root/children


I am writing a window's application to download files from personal and work onedrive accounts.

The same request works for work account, but not for personal account.

Not sure if this has anything to do with settings.

The redirect url for personal account is login.live.com/oauth20_desktop.srf.

Authentication is done through: login.microsoftonline.com/common/oauth2/

Request for both personal and work accounts:

requestMessage = {Method: GET, RequestUri: 'https://graph.microsoft.com/v1.0/me/drive/root/children?pretty=false', Version: 1.1, Content: <null>, Headers:
{
  Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFEUk5ZUlEzZGhSU3JtLTRLLWFkcENKVm1pX2Fvb1NzcThPZ1g...

Response from personal account:

httpResponse = {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  request-id: 843007b8-ebe4-4c9c-b260-d2a92512b672
  client-request-id: 843007b8-ebe4-4c9c-b260-d2a92512b672
  ...

Response from work account:

httpResponse = {StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  Vary: Accept-Encoding
  request-id: a27688cb-fc5f-4e8c-8a9b-7a3ce1e4c98c
  client-request-id: a27688cb-fc5f-4e8c-8a9b-...

Solution

  • If you want to use Microsoft Graph with personal and business OneDrive, you need to use the Azure Active Directory OAuth v2 endpoints

    https://login.microsoftonline.com/common/oauth/v2.0/authorize
    https://login.microsoftonline.com/common/oauth/v2.0/token
    

    These will handle determining if the user is a Personal or Work account, and then getting an access token that Microsoft Graph can use.

    There are more details available about this: https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-oauth-code/