[英]Failed to acquire token with authorization code using Microsoft graph API with Java Spring
我按照microsoft docs中的指南直接在浏览器中输入URL时成功获取了授权码。 但是如果我启动 Spring 应用程序并执行相同的操作,它会说:[invalid_request]。 所以我做了以下操作:在启动 Spring 应用程序之前获取授权码,然后使用以下代码运行 Spring 应用程序:
final AuthorizationCodeCredential authCodeCredential = new AuthorizationCodeCredentialBuilder()
.clientId("1234")
.clientSecret("4567") //required for web apps, do not set for native apps
.authorizationCode("abcd12345")
.redirectUrl("http://localhost:8080/login/oauth2/code/azure")
.build();
// List<String> scopes = Arrays.asList("https://graph.microsoft.com/mail.read".split(","));
List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default".split(","));
final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, authCodeCredential);
final GraphServiceClient graphClient =
GraphServiceClient
.builder()
.authenticationProvider(tokenCredentialAuthProvider)
.buildClient();
Message message = new Message();
message.subject = "Meet for lunch?";
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = "The new cafeteria is open.";
message.body = body;
LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "opoiuiotqq@gmail.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
boolean saveToSentItems = false;
graphClient.me()
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(saveToSentItems)
.build())
.buildRequest()
.post();
它只是弹出一条错误消息:ERROR com.azure.identity.AuthorizationCodeCredential - Azure Identity => ERROR in getToken() call for scope [https://graph.microsoft.com/.default]:无法使用授权代码获取令牌。
所以我想是不是和我一开始提到的问题有关。 这是指南: https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-authorization-code
这可能无法准确回答您的问题,但正如 DOC 中所述,您可以使用 DOC 中提到的两种方式兑换访问令牌的代码,即
1)使用client_secret请求访问令牌-https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-access-token-与客户秘密
2)用证书凭据请求访问令牌-https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-access-token -with-a-certificate-credential 你能试试这个吗,希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.