0

因此,当我尝试进行身份验证时,它给了我一个访问令牌,但是当使用该令牌发送邮件时,它会抛出一个错误

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (401)Unauthorized
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74)
    at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
    at microsoft.exchange.webservices.data.core.ExchangeService.internalCreateItems(ExchangeService.java:598)
    at microsoft.exchange.webservices.data.core.ExchangeService.createItem(ExchangeService.java:657)
    at microsoft.exchange.webservices.data.core.service.item.Item.internalCreate(Item.java:245)
    at microsoft.exchange.webservices.data.core.service.item.EmailMessage.internalSend(EmailMessage.java:147)
    at microsoft.exchange.webservices.data.core.service.item.EmailMessage.send(EmailMessage.java:258)
    at microsoft.exchange.webservices.data.core.service.item.EmailMessage$send$0.call(Unknown Source)

我使用以下结构来获取访问令牌

 "https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token", [
        grant_type         : "client_credentials",
        client_id          : clientId,
        client_secret      : secret,
        scope              : "https://outlook.office365.com/.default",
        requested_token_use: 'on_behalf_of'
],
        [
                "Content-Type": "application/x-www-form-urlencoded"
        ] 

使用java 插件

service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
service.setTraceEnabled(true)
service.setUrl(new URI("https://outlook.office365.com/ews/exchange.asmx"))


service.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email))
service.setCredentials(new AuthCredentials(accessToken, email))

但这在尝试发送电子邮件时都会引发未经授权的错误

4

0 回答 0