授权客户端浏览Office365 Graph API

pas*_*ine 14 active-directory azure office365

我正在尝试开发一个webapp,让用户浏览他的Active Directory联系人.
我有两个帐户:一个用于注册应用程序(开发者帐户),另一个用户是可以访问Office365(用户帐户)的普通用户.
我已经按照Azure AD Graph API文档设置了一个Javascript客户端.
到现在为止,我能够提示用户登录并检索访问令牌,但是当我尝试发出请求时,我总是收到401错误.
我是Azure的新手,所以我真的不明白问题是在我的应用程序配置中还是在我的代码中.
我可以使用我的用户帐户浏览Graph API资源管理器,因此我认为他没有错过访问它的授权.
我真的很困惑.

我尝试添加我正在做的所有步骤,希望有人可以指出错误.

要求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
Run Code Online (Sandbox Code Playgroud)

回复1:

code           // A long string
state          // The string I sent in the request
session_state  // Another string
Run Code Online (Sandbox Code Playgroud)

要求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
Run Code Online (Sandbox Code Playgroud)

回应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
Run Code Online (Sandbox Code Playgroud)

要求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.
Run Code Online (Sandbox Code Playgroud)

回应3:

{
    "odata.error": {
        "code": "Authentication_MissingOrMalformed",
        "message": {
            "lang": "en",
            "value": "Access Token missing or malformed."
        },
        "values": null
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的访问令牌的内容:

{
 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: "myuseremail@contoso.com",
 unique_name: "myuseremail@contoso.com",
 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"
}
Run Code Online (Sandbox Code Playgroud)

回答

因此,看起来用户必须具有"可以同意"授权才能对其自己的活动目录进行身份验证,这可以由管理员提供.
我在MSDN论坛上发布了相同的请求,我得到了相同的答案.
我真诚地感谢@Jason Johnston和@Dan Kershaw,因为这个问题让我感到疯狂,如果没有他们的帮助,我将永远无法解决问题.
不幸的是,我可以奖励他们中的一个,所以我决定给@Jason Johnston,因为他在这个和另一个讨论中支持我的极大耐心.

Jas*_*ton 3

我相信,如果您确实想要浏览 Active Directory,而不仅仅是读取经过身份验证的用户的个人资料,则需要获得 Web 应用程序的管理员同意。请参阅http://msdn.microsoft.com/en-us/library/azure/b08d91fa-6a64-4deb-92f4-f5857add9ed8#BKMK_Graph

如果您已经知道这一点,那么可能是您注册应用程序或令牌本身的方式存在问题。确保您已在应用程序注册中为每个链接选择了适当的权限。如果这些看起来正确,那么您可以检查令牌。这里有一个方便的小标记解析器: http: //jwt.calebb.net/。只需粘贴您的令牌值,它就会显示解码后的 JSON。查看范围或 scp 参数。

{
  "typ": "JWT",
  "alg": "RS256",
  "x5t": "asdfsadfasdfsa"
}

{
  "aud": "https://graph.windows.net/",
  "iss": "https://sts.windows.net/<SOMEGUID>",
  "iat": 1418158549,
  "nbf": 1418158549,
  "exp": 1418162449,
  "ver": "1.0",
  "tid": "<SOMEGUID>",
  "amr": [
    "pwd"
  ],
  "oid": "<SOMEGUID>",
  "upn": "admin@contoso.com",
  "unique_name": "admin@contoso.com",
  "sub": "askdljalsdfs",
  "puid": "1003BFFD88937280",
  "family_name": "Administrator",
  "given_name": "MOD",
  "appid": "<YOUR APP ID>",
  "appidacr": "0",
  "scp": "Directory.Read user_impersonation UserProfile.Read",
  "acr": "1"
}
Run Code Online (Sandbox Code Playgroud)