【发布时间】:2019-07-31 13:40:50
【问题描述】:
Active Directory Authentication Library for Java (ADAL4J) 允许通过对Microsoft Graph API 的访问令牌进行身份验证,使用以下(简化)代码:
public String authenticate(String authorizationUrl, String clientId, String clientSecret) throws Exception {
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authorizationUrl, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = context.acquireToken(“https://graph.microsoft.com”, credential, null);
return future.get().getAccessToken();
}
以上内容适用于 Graph 的某些部分(例如,用于访问 Office 365 帐户),但不适用于 OneDrive,它返回一个没有适当授权的访问令牌。
通过POSTMAN 获取访问令牌按预期工作,使用以下参数:
authorizationUrl: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
accessTokenUrl: https://login.microsoftonline.com/common/oauth2/v2.0/token
clientId: <the clientId for the application>
clientSecret: <the clientSecret for the application>
scope: https://graph.microsoft.com/.default
state: <empty>
更具体地说,在POSTMAN 中运行上述内容会返回一个具有其他范围的访问令牌,包括https://graph.microsoft.com/Files.ReadWrite.All。在调用上述authenticate() 方法的Java 应用程序中使用该访问令牌确实有效,例如它使用/me/drive/root/children 作为 REST 路径列出了根目录的内容。
但是,如果使用authenticate() 方法返回的访问令牌,则OneDrive 会返回错误。如果在 authenticationUrl 中使用特定租户 ID 而不是 common,则从路径中删除用户名 (me) 仅返回 1 个文件名。
似乎无法在 ADAL4J 中添加范围值,并且许多其他变体要么导致错误,要么导致返回 1 个文件(可能来自不同的上下文)。
有没有办法通过 ADAL4J 为 OneDrive 获取完全授权的访问令牌?
【问题讨论】:
标签: java authentication onedrive adal adal4j