Search code examples
access-tokenonedrive

Another user onedrive files using access token


I'm userX. I need to access userY onedrive files from api and upload files.

When I send request for user token to https://login.microsoftonline.com/tenant/oauth2/token I got it.
POST token Request:
   url: https://login.microsoftonline.com/tenant/oauth2/token
   grant_type: password
   username: userY
   password: ***(my password)***
   resource: ***(my resource)***
   client_id: ***(my client id)***
   client_secret: ***(my client secret)***
Response:
   "token_type": "Bearer",
   "scope": "AllSites.FullControl AllSites.Manage AllSites.Read AllSites.Write Calendars.Read Calendars.Read.Shared Calendars.ReadWrite Calendars.ReadWrite.Shared Contacts.Read Contacts.Read.Shared Contacts.ReadWrite Contacts.ReadWrite.Shared Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All email Files.Read Files.Read.All Files.Read.Selected Files.ReadWrite Files.ReadWrite.All Files.ReadWrite.AppFolder Files.ReadWrite.Selected Group.Read.All Group.ReadWrite.All IdentityRiskEvent.Read.All Mail.Read Mail.Read.Shared Mail.ReadWrite Mail.ReadWrite.Shared Mail.Send Mail.Send.Shared MailboxSettings.ReadWrite Member.Read.Hidden MyFiles.Read MyFiles.Write Notes.Create Notes.Read Notes.Read.All Notes.ReadWrite Notes.ReadWrite.All Notes.ReadWrite.CreatedByApp offline_access openid People.Read profile Reports.Read.All Sites.Read.All Sites.ReadWrite.All Sites.Search.All Tasks.Read Tasks.Read.Shared Tasks.ReadWrite Tasks.ReadWrite.Shared TermStore.Read.All TermStore.ReadWrite.All User.Read User.Read.All User.ReadBasic.All User.ReadWrite User.ReadWrite.All",
  "expires_in": "3599",
  "ext_expires_in": "0",
  "expires_on": "1485157695",
  "not_before": "1485153795",
  "resource": ***(my resource)***
  "access_token": "***here is my access token***"
  "refresh_token": "***here is my refresh token***"


I try to use this token:

First example (is not appropriate: Kevin explained below):
    GET Request
    url: https://api.office.com/discovery/v2.0/me/services
    Header Authorization: Bearer  ***here is my access token***
    Response:
    {
    "error": {
    "code": "-2147024891, System.UnauthorizedAccessException",
    "message": "Access denied. You do not have permission to perform this action or access this resource."
    }
    }

Second example:
    GET Request
    url: http://tenant.sharepoint.com/_api/search/query?querytext='*'
    Header Authorization: Bearer  ***here is my access token***
    Response:
    {"error_description":"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}


Third example: 
    POST Request:
    url: https://tenant-my.sharepoint.com/_api/v2.0
    Header Authorization: Bearer  ***here is my access token***
    Response:
    {"error_description":"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}


The question is: why I can't use my access token? What I'm doing wrong?

Thanks

Solution

  • Your first example is a request to the Discovery service, which ordinarily is used to discover the resource ID if you don't already know it. This request will require a token acquired with resourceId == the discovery service URL. Once you have obtained the correct resource ID from the discovery service, you can request a new access token with the new resourceId.

    "The question is: why I can't use my refresh token? What I'm doing wrong?": The refresh token is not a valid token for making API calls, you need the access_token for that. The refresh token is used to get a new access token when it expires.