我没有在我这边重现您的问题。我按照本教程成功获取Authentication code
和获取access token
并refresh token
使用以下代码。请参考它。
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class GetTokenByAuthenticationCode {
private static final String APP_ID = "***";
private static final String APP_SECRET = "***";
private static final String REDIRECT_URI = "http://localhost:8080";
private static final String tenant = "***";
public static void main(String[] args) throws Exception {
String authority = "https://login.microsoftonline.com/" + tenant + "/oauth2/authorize";
ExecutorService service = Executors.newFixedThreadPool(1);
String code = "***";
AuthenticationContext context = new AuthenticationContext(authority, true, service);
URI url = new URI(REDIRECT_URI);
Future<AuthenticationResult> result = context.acquireTokenByAuthorizationCode(
code,
url,
new ClientCredential(APP_ID, APP_SECRET),
null
);
String token = result.get().getAccessToken();
System.out.println(token);
String refreshToken = result.get().getRefreshToken();
System.out.println(refreshToken);
Future<AuthenticationResult> result1 = context.acquireTokenByRefreshToken(
refreshToken,
new ClientCredential(APP_ID, APP_SECRET),
null
);
String tokenNew = result1.get().getAccessToken();
String refreshTokenNew = result1.get().getRefreshToken();
System.out.println(tokenNew);
System.out.println(refreshTokenNew);
}
}
解码:
更新答案:
首先,很抱歉这个错误。我换成getIdToken
了getAccessToken
,结果和你一样。然后我在Authorize access to Azure Active Directory web applications using OAuth 2.0 code grant flow中搜索了响应参数,你可以找到id_token
参数的语句。
表示 ID 令牌的未签名 JSON Web 令牌 (JWT)。应用程序可以 base64Url 解码此令牌的片段以请求有关登录用户的信息。应用程序可以缓存值并显示它们,但不应依赖它们来获得任何授权或安全边界。
因此,id 令牌只是一个不能依赖的段。如需获取完整的 id 令牌,请参考openId 流程。