Sto cercando di sviluppare una webapp per consentire a un utente di sfogliare i suoi contatti di Active Directory.
Ho due account: uno per registrare l'applicazione (account sviluppatore) e l'altro che sarebbe l'utente generale che ha accesso a Office365 (account utente).
Ho creato un client Javascript seguendo lo Azure AD Graph API documentation.
A questo punto, sono in grado di richiedere all'utente di accedere e recuperare un token di accesso, ma quando provo a effettuare una richiesta, ottengo sempre un errore 401.
Sono abbastanza nuovo ad Azure, quindi non capisco se il problema è nella configurazione dell'applicazione o nel mio codice.
Sono in grado di cercare il Graph API explorer con il mio account utente, quindi non penso che manchi l'autorizzazione per accedervi.
Sono davvero confuso.Autorizzare un client a consultare l'API grafico Office365
Cerco di aggiungere tutti i passaggi che sto facendo, sperando che qualcuno possa segnalare l'errore.
Richiesta 1:
Url: https://login.windows.net/{my tenant id or common (both are working) }/oauth2/authorize
Method: GET
Params:
redirect_uri // my redirect url, the same I registered in my application. It is just a page that returns the content of the URL
client_id // my client id
response_type // code
state // a random generated string, not required, but reccomanded
resource // https://graph.windows.net
Risposta 1:
code // A long string
state // The string I sent in the request
session_state // Another string
Richiesta 2:
Url: https://login.windows.net/{my tenant id or common (both are working) }/oauth2/token
Method: POST
Params:
redirect_uri // it won't be necessary, but in some post they reccomand to add it
client_id // my client id
client_secret // my client secret
code // the code retrieved from Request 1
grant_type // authorization_code
state // a random generated string
resource // https://graph.windows.net
Risposta 2:
token_type // Bearer
access_token // a long token
id_token // exploring it with the JWT tool, shows it has the correct app id
refresh_token // a long code
resource // the same I sent in the request
scope // Directory.Read UserProfile.Read
expires_in
expires_on
a couple of other irrelevant keys
Richiesta 3:
Url: https://graph.windows.net/{the domain the logged account belong to}/contacts
Method: GET
Headers:
Authorization: Bearer {the access token retrieved from request 2}
Params:
api-version = 1.5 // The value suggested in the documentation.
Risposta 3:
{
"odata.error": {
"code": "Authentication_MissingOrMalformed",
"message": {
"lang": "en",
"value": "Access Token missing or malformed."
},
"values": null
}
}
Questo è il contenuto del mio token di accesso:
{
typ: "JWT",
alg: "RS256",
x5t: "foofoofoofoo"
}.
{
aud: "https://graph.windows.net",
iss: "https://sts.windows.net/<SOMEGUID>/",
iat: 1418224761,
nbf: 1418224761,
exp: 1418228661,
ver: "1.0",
tid: "<SOMEGUID>",
amr: [
"pwd"
],
idp: "https://sts.windows.net/<SOMEGUID>/",
email: "[email protected]",
unique_name: "[email protected]",
sub: "barbarbarbar",
altsecid: "<an-id>",
family_name: "Last Name",
given_name: "First Name",
appid: "<MY APP ID>",
appidacr: "1",
scp: "Directory.Read UserProfile.Read",
acr: "1"
}
Risposta
Quindi, sembra che l'utente debba disporre dell'autorizzazione "può consentire" di autenticarsi nella propria directory attiva, e questo può essere fornito solo dall'amministratore.
Ho inviato la stessa richiesta sul forum MSDN e ho ottenuto la stessa risposta.
Desidero sinceramente ringraziare @Jason Johnston e @Dan Kershaw poiché questo problema mi stava facendo impazzire e non sarei mai stato in grado di risolverlo senza il loro aiuto.
Purtroppo posso assegnare la taglia a solo uno di loro, così ho deciso di darlo @Jason Johnston per la grande pazienza che ha avuto nel supportarmi in questa e in un'altra discussione.
L'ambito di ritorno quando richiedo un token di accesso è 'Directory.Read user_impersonation UserProfile.Read': dovrebbe essere anche all'interno di token_id? – pasine
No, non penso. Prova a eliminare la voce user_impersonation, che è l'autorizzazione "Accesso completo alla cassetta postale" in Exchange. Tale autorizzazione non è compatibile con le apis REST. –
Stesso problema: '401 Access Token mancante o non valido. – pasine