【问题标题】:Authorize a client to browse Office365 Graph API授权客户端浏览 Office365 Graph API
【发布时间】:2014-12-08 00:55:46
【问题描述】:

我正在尝试开发一个 web 应用程序来让用户浏览他的 Active Directory 联系人。
我有两个帐户:一个用于注册应用程序(开发人员帐户),另一个是有权访问 Office365 的普通用户(用户帐户)。
我在Azure AD Graph API documentation 之后设置了一个 Javascript 客户端。
到目前为止,我可以提示用户登录并检索访问令牌,但是当我尝试发出请求时,总是收到 401 错误。
我对 Azure 还很陌生,所以我真的不明白问题出在我的应用程序配置中还是在我的代码中。
我可以使用我的用户帐户浏览Graph API explorer,所以我认为他没有丢失访问它的授权。
我真的很困惑。

我尝试添加我正在执行的所有步骤,希望有人能指出错误。

请求 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

响应 1:

code           // A long string
state          // The string I sent in the request
session_state  // Another string

请求 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

回应 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

请求 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.

回应 3:

{
    "odata.error": {
        "code": "Authentication_MissingOrMalformed",
        "message": {
            "lang": "en",
            "value": "Access Token missing or malformed."
        },
        "values": null
    }
}

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

{
 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"
}

回答

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

【问题讨论】:

    标签: azure active-directory office365


    【解决方案1】:

    我相信,如果您真的想浏览 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"
    }
    

    【讨论】:

    • 我请求访问令牌时的返回范围是Directory.Read user_impersonation UserProfile.Read:它也应该在token_id内吗?
    • 不,我不这么认为。尝试取出 user_impersonation 条目,这是 Exchange 下的“完全访问邮箱”权限。该权限与 REST api 不兼容。
    • 同样的问题:401 Access Token missing or malformed.
    • 为了确定,您将令牌放在请求的授权标头中,例如“授权:承载 ”,对吗?
    • 好的。您的令牌的 appid 参数是否与您的应用匹配? aud参数的值是多少?
    【解决方案2】:

    您粘贴的令牌缺少 OID 和 UPN,这可能就是您看到此错误的原因。我得回去检查一下如果没有这些声明,这个访问令牌是如何发出的。

    【讨论】:

    • 两者都需要吗?服务器怎么可能返回一个不完整的令牌?这才是真正让我困惑的地方。
    • 这里也一样。我不确定这是怎么发生的。需要让其他一些人参与进来。
    • 抱歉延迟回复。您是否使用 ADAL 获取访问令牌?如果是这样,你能分享那个代码sn-p吗?我们怀疑您正在使用您的应用注册位置(开发者租户)的tenantId 配置 STS URL(显示在令牌中的 iss)。实际上,无论用户来自哪个租户,您总是尝试使用您的开发者租户让他们登录,而这些用户在开发者租户中并不存在。您可以尝试使用公共端点而不是特定于租户的端点吗? tid 和 iss 匹配吗? idp 和 altsecid 不应存在
    • 因此,行为如下:如果我尝试在登录前向公共端点运行请求,我将重定向到用户登录,登录后我收到一条错误消息,提示我不是管理员。如果我使用我的开发者端点而不是普通端点重做请求,我会得到验证码。如果我随后使用公共端点重新执行请求,我将获得 Auth Code。使用此代码检索的令牌有 iss、tid、idp 和 altsecid。 tid 和 iss 匹配。
    • 我从来没有意识到在公共端点上运行第一个请求会引发错误。
    猜你喜欢
    • 1970-01-01
    • 2020-09-23
    • 2013-06-18
    • 2014-11-18
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    相关资源
    最近更新 更多