gpt4 book ai didi

java - Azure:无法使用 RefreshToken 获取新的 AccessToken

转载 作者:太空狗 更新时间:2023-10-29 22:41:38 32 4
gpt4 key购买 nike

我正在构建一个需要访问我们客户的 Office 365 Management Activities 的应用程序。我已按照this Azure Active Directory中概述的步骤进行操作概述,并且能够使用 OAuth 代码获取初始访问 token ,以及使用此 token 设置 O365 订阅。

但是,当我使用初始 token 提供的 refresh_token 来获取新的访问 token 时,出现以下错误:

{"error_description":"AADSTS65001: The user or administrator has not consented to use the application with ID '8f72f805-dfd2-428d-8b0e-771a98d26c16'. Send an interactive authorization request for this user and resource.\r\nTrace ID: df229c3f-8f28-420b-9ac3-321ab1b2ad09\r\nCorrelation ID: 0e0f2bcb-4b19-458a-8556-2a6d4e51379f\r\nTimestamp: 2016-10-03 17:33:20Z","error":"invalid_grant"}

由于我能够获取并使用初始访问 token ,因此我非常确定用户正在授予我的应用程序一些权限。为了使用刷新 token 获取新的访问 token ,我是否需要特定的权限?

编辑:具体来说,我正在使用 com.microsoft.azure::adal4j java package ,AuthenticationContext 类,acquireTokenByAuthorizationCodeacquireTokenByRefreshToken方法:

public class AzureProvisioner {
private final AuthenticationContext authService = new AuthenticationContext(
"https://login.windows.net/common/oauth2/token", true, Executors.newSingleThreadExecutor());
private final ClientCredential clientCredential = new ClientCredential("azureAppId", "azureAppSecret");
public static final String resource = "https://manage.office.com";
// Internal implementation of REST interface; Microsoft didn't provide a Java Library
final Office365ManagementApi managementApi;

public void acquireToken(final String authCode, final URI redirectUri) {
final AuthenticationResult authResult = authService.acquireTokenByAuthorizationCode(
authCode, redirectUri, clientCredential, resource, null).get()
// internal library code, gets the "tid" field from parsing the JWT token
final String tenantId = JwtAccessToken.fromToken(authResult.getAccessToken()).getTid();

// works
createInitialSubscription(customerId, authResult.getAccessToken(), tenantId);

// throws an error
final AuthenticationResult refreshResult = authService.acquireTokenByRefreshToken(
authResult.getRefreshToken(), clientCredential, null).get();
}

private void createInitialSubscription(final String accessToken, final String tenantId) {
final String authHeader = "Authorization: Bearer " + accessToken;
final String contentType = "Audit.AzureActiveDirectory";
// internal implementation
final CreateWebhookRequest requestBody = new CreateWebhookRequest();
managementApi.createSubscription(authHeader, tenantId, contentType, requestBody);
}
}

相同的代码,没有任何外部依赖,对我来说也不起作用:

public class AzureProvisioner {
private final AuthenticationContext authService = new AuthenticationContext(
"https://login.windows.net/common/oauth2/token", true, Executors.newSingleThreadExecutor());
private final ClientCredential clientCredential = new ClientCredential("8f72f805-dfd2-428d-8b0e-771a98d26c16", "secret");
public final String resource = "https://manage.office.com";
private URI redirectUri = new URI("https://localhost");

private static final String oAuthUrl = "https://login.windows.net/common/oauth2/authorize?response_type=code&client_id=8f72f805-dfd2-428d-8b0e-771a98d26c16&resource=https%3A%2F%2Fmanage.office.com&redirect_uri=https%3A%2F%2Flocalhost";

public AzureProvisioner() throws Exception {
// do nothing
}

public static void main(String... args) throws Exception {
final String authCode = "AQABAAAAAADRNYRQ3dhRSrm...";
new AzureProvisioner().acquireToken(authCode);
}

public void acquireToken(final String authCode) throws Exception {
final AuthenticationResult authResult = authService.acquireTokenByAuthorizationCode(
authCode, redirectUri, clientCredential, resource, null).get();
System.out.println(authResult.getAccessToken());

// throws an error
final AuthenticationResult refreshResult = authService.acquireTokenByRefreshToken(
authResult.getRefreshToken(), clientCredential, resource, null).get();
System.out.println(refreshResult.getAccessToken());
}
}

使用代理,我跟踪了 https 刷新请求:

Method: POST
Protocol-Version: HTTP/1.1
Protocol: https
Host: login.windows.net
File: /common/oauth2/token
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 876

refresh_token={token}
&resource=https%3A%2F%2Fmanage.office.com
&grant_type=refresh_token
&scope=openid
&client_secret={secret}
&client_id=8f72f805-dfd2-428d-8b0e-771a98d26c16

最佳答案

事实证明,根本问题在于我的应用程序权限。在我的应用程序>设置>所需权限> Office 365管理API下,我选择了“应用程序权限”,我需要在其中选择“委派权限”。将它们交换过来,我的代码立即开始按预期工作。

wrong!

关于java - Azure:无法使用 RefreshToken 获取新的 AccessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39671471/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com