Microsoft Graph API - 无法刷新访问令牌
Microsoft Graph API - cannot refresh access token
我已经在 https://apps.dev.microsoft.com 注册了一个多租户应用程序,因为 "admin consent" 提示在 Azure AD 应用程序中不可用。我们的应用程序需要管理员同意才能检索有关用户及其日历的信息。
我可以从一个完全不同的租户提供管理员许可,而不是从注册此应用程序的租户,并使用提供的访问令牌来检索所有必要的信息,但是显然一小时后过期,我们需要离线访问。
我曾尝试在 https://login.windows.net/common/oauth2/token
端点中使用 tenantId 而不是 'common',但是收到了如下相同的消息。
以下是提交给token端点的json格式的数据(提交前在节点内转换成编码格式):
{
grant_type: 'refresh_token',
client_id: 'e5c0d59d-b2c8-4916-99ac-3c06d942b3e3',
client_secret: '(redacted)',
refresh_token: '(redacted)',
scope: 'openid offline_access calendars.read user.read.all'
}
当我尝试刷新访问令牌时收到错误消息:
{
"error":"invalid_grant",
"error_description":"AADSTS65001: The user or administrator has not consented to use the application with ID 'e5c0d59d-b2c8-4916-99ac-3c06d942b3e3'. Send an interactive authorization request for this user and resource.\r\nTrace ID: 2bffaa08-8c56-4872-8f9c-985417402e00\r\nCorrelation ID: c7653601-bf96-46c3-b1ff-4857fb25b7dc\r\nTimestamp: 2017-03-22 02:17:13Z",
"error_codes":[65001],
"timestamp":"2017-03-22 02:17:13Z",
"trace_id":"2bffaa08-8c56-4872-8f9c-985417402e00",
"correlation_id":"c7653601-bf96-46c3-b1ff-4857fb25b7dc"
}
即使使用标准同意,也会出现此错误。我也尝试使用 node-adal 库而不是产生完全相同结果的原始 http 请求。
我注意到 "offline_access" 不是我能够在 MS 应用程序门户中设置的权限,但是我猜我正在获取刷新令牌这一事实意味着我可以刷新访问权限令牌?
作为记录,以下是我用来查看是否做错的node-adal代码:
var self = this;
var authenticationContext = new AuthenticationContext('https://login.windows.net/common');
authenticationContext.acquireTokenWithRefreshToken(
self.refreshToken,
self.clientId,
self.clientSecret,
'https://graph.microsoft.com/',
function(a) {
console.log(a);
}
);
感谢任何帮助刷新过程的工作!
请确保您用于刷新令牌的租户与您为 access_token 请求的租户相同。
除非出现以下情况,否则刷新令牌请求对我来说效果很好:
- 使用 Microsoft 帐户从 protal 注册应用程序
- 用户 1 在租户 1 中
- 将用户 1 添加为租户 2 的外部用户
- 向租户 1 请求 access_token/refresh_token(确定)
- 尝试在请求中使用 tenant1 刷新令牌(OK)
- 尝试在请求中使用 tenant2 刷新令牌(相同的错误消息)
我已经在 https://apps.dev.microsoft.com 注册了一个多租户应用程序,因为 "admin consent" 提示在 Azure AD 应用程序中不可用。我们的应用程序需要管理员同意才能检索有关用户及其日历的信息。
我可以从一个完全不同的租户提供管理员许可,而不是从注册此应用程序的租户,并使用提供的访问令牌来检索所有必要的信息,但是显然一小时后过期,我们需要离线访问。
我曾尝试在 https://login.windows.net/common/oauth2/token
端点中使用 tenantId 而不是 'common',但是收到了如下相同的消息。
以下是提交给token端点的json格式的数据(提交前在节点内转换成编码格式):
{
grant_type: 'refresh_token',
client_id: 'e5c0d59d-b2c8-4916-99ac-3c06d942b3e3',
client_secret: '(redacted)',
refresh_token: '(redacted)',
scope: 'openid offline_access calendars.read user.read.all'
}
当我尝试刷新访问令牌时收到错误消息:
{
"error":"invalid_grant",
"error_description":"AADSTS65001: The user or administrator has not consented to use the application with ID 'e5c0d59d-b2c8-4916-99ac-3c06d942b3e3'. Send an interactive authorization request for this user and resource.\r\nTrace ID: 2bffaa08-8c56-4872-8f9c-985417402e00\r\nCorrelation ID: c7653601-bf96-46c3-b1ff-4857fb25b7dc\r\nTimestamp: 2017-03-22 02:17:13Z",
"error_codes":[65001],
"timestamp":"2017-03-22 02:17:13Z",
"trace_id":"2bffaa08-8c56-4872-8f9c-985417402e00",
"correlation_id":"c7653601-bf96-46c3-b1ff-4857fb25b7dc"
}
即使使用标准同意,也会出现此错误。我也尝试使用 node-adal 库而不是产生完全相同结果的原始 http 请求。
我注意到 "offline_access" 不是我能够在 MS 应用程序门户中设置的权限,但是我猜我正在获取刷新令牌这一事实意味着我可以刷新访问权限令牌?
作为记录,以下是我用来查看是否做错的node-adal代码:
var self = this;
var authenticationContext = new AuthenticationContext('https://login.windows.net/common');
authenticationContext.acquireTokenWithRefreshToken(
self.refreshToken,
self.clientId,
self.clientSecret,
'https://graph.microsoft.com/',
function(a) {
console.log(a);
}
);
感谢任何帮助刷新过程的工作!
请确保您用于刷新令牌的租户与您为 access_token 请求的租户相同。
除非出现以下情况,否则刷新令牌请求对我来说效果很好:
- 使用 Microsoft 帐户从 protal 注册应用程序
- 用户 1 在租户 1 中
- 将用户 1 添加为租户 2 的外部用户
- 向租户 1 请求 access_token/refresh_token(确定)
- 尝试在请求中使用 tenant1 刷新令牌(OK)
- 尝试在请求中使用 tenant2 刷新令牌(相同的错误消息)