2017-01-26 2 views
0
 
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 

ответ

0

Ваш первый пример - это запрос к службе обнаружения, который обычно используется для обнаружения идентификатора ресурса, если вы еще этого не знаете. Для этого запроса потребуется токен, полученный с помощью resourceId == URL-адреса службы обнаружения. После того как вы получили правильный идентификатор ресурса из службы обнаружения, вы можете запросить новый токен доступа с новым ресурсом.

Вопрос: почему я не могу использовать токен обновления? Что я делаю неправильно? »: Ток обновления - недействительный токен для вызовов API, для этого вам нужен access_token. Маркер обновления используется для получения нового токена доступа, когда он истекает.

+0

Thanks Kevin. Как я вижу, 1-й пример не один из лучших. Я понимаю свою ошибку - я пытался использовать токен доступа из моего ресурса. Я должен использовать токен доступа для URL-адреса службы обнаружения. Вопрос «почему я не могу использовать токен обновления» должен быть «почему я не могу использовать токен доступа», и это актуально для остальных 2 примеров. – Krzysiek

0
 
Solved! 
The problem with my requests is "resource: ***(my resource)***". 
If I want to access to resource I can't use app id or client id (in my case those two were the same). 
Which resource I want - I need token for it. I should read from response for https://api.office.com/discovery/v2.0/me/services list of available resources 

So 

1. POST https://login.microsoftonline.com/tenant/oauth2/token with resource "https://api.office.com/discovery/" 
2. GET https://api.office.com/discovery/v2.0/me/services with refresh_token from point 1 
3. POST https://login.microsoftonline.com/tenant/oauth2/token with resource "https://tenant-my.sharepoint.com/" in my case 
4. And use refresh token from point 3 e.g. GET https://tenant-my.sharepoint.com/_api/v2.0/me/drive/special/documents 

Смежные вопросы