【问题标题】:Invalid token received from adal.js从 adal.js 收到的令牌无效
【发布时间】:2015-05-29 13:36:44
【问题描述】:

我正在尝试使用adal.js(目前没有角度部分)创建一个小型客户端 web 应用程序。

我已经成功登录并获得了一个经过身份验证的用户对象。但是,我显然从 API 获得了无效的 OAuth 令牌。

我发送到 AuthenticationContext 实例的配置:

var config = {
    instance: 'https://login.microsoftonline.com/',
    tenant: 'dbaa88b3-...',
    clientId: 'f81bede8-...',
    postLogoutRedirectUri: window.location.href,
    cacheLocation: 'localStorage', 
};

不确定是租户 ID 还是租户名称?

然后当我通过身份验证时,我正在使用

var context = new AuthenticationContext(config);
context.acquireToken(context.config.clientId, function(error, token) {
       // error is null
       var now = new Date();
       $.ajax({
          url: "https://outlook.office365.com/api/v1.0/me/calendarview",
          method: "GET",
          data: {
             "startdatetime": (new Date()).toISOString(),
             "enddatetime": (new Date()).toISOString()
          },
          headers: {
             "Authorization": "Bearer " + token,
             "Accept": "application/json; odata.metadata=full",
             "Client-Request-Id": clientid, // Some unique machine id?
             "User-Agent": navigator.userAgent,
             "Date": now.toUTCString()
          }
       }).then(function(result) {},
       function(e) {
          console.error(e);
          // 401 response, x-ms-diagnostics header = 2000005;reason="The PUID value was not found for [ORGID] identity.";error_category="invalid_user"
       });     
});

如果我在https://oauthplay.azurewebsites.net/ 上手动输入使用相同帐户生成的身份验证令牌,它会成功。

【问题讨论】:

  • 你能用这个网站来解码这两个令牌并比较:jwt.io

标签: javascript azure oauth azure-active-directory adal


【解决方案1】:

tl;dr 如果您想要访问令牌,请不要将客户端 ID 作为第一个参数发送给 acquireToken

好像我用错了库。

当调用acquireToken并且您想要访问令牌时,您不应该将clientId作为第一个参数提供,那么您将获得标识令牌(能够使用http://jwt.io/#工具解决这个问题,感谢@rich-randall)。至少我是这样解释code的。

所以我向 API 提供了标识而不是访问令牌,它不包含“puid”值,因此出现错误消息:

// 401 response, x-ms-diagnostics header = 2000005;reason="The PUID value was not found for [ORGID] identity.";error_category="invalid_user"

现在我可以从 API 中提取数据了。

【讨论】:

  • 如果不是clientId,那么需要传递什么。把它留空是不行的
  • 请端到端发布您的完整解决方案。获取有效令牌调用图 api 时存在多个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2019-08-13
  • 2015-10-08
  • 2019-07-16
相关资源
最近更新 更多