远程服务器返回错误:(421)-尝试访问Office 365邮箱时出现此错误

g6ll5ycj  于 2022-09-21  发布在  其他
关注(0)|答案(4)|浏览(1135)

我已经创建了一个独立的NetBeans Java 8.0应用程序,它使用EWS连接到Office 365邮箱。我已经在Azure目录中注册了相同的内容。目前使用基本身份验证,我可以从Office 365邮箱下载邮件并检查附件。但当我使用访问令牌将其更改为xoauth方法时,我得到了上面提到的错误(远程服务器返回错误:(421))。你能帮我解决这个问题吗?我使用了下面的代码。

IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET);
            ConfidentialClientApplication app = ConfidentialClientApplication
                    .builder(PUBLIC_CLIENT_ID, credential)
                    .authority(AUTHORITY)
                    .build();

Set<String> scopes = new HashSet<String>();
scopes.add("api://"+PUBLIC_CLIENT_ID"/.default");

 ClientCredentialParameters clientCredentialParameters = ClientCredentialParameters.builder(scopes).build();
             CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParameters);

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

 BearerAccessToken bat = new BearerAccessToken(future.get().accessToken());
ExchangeCredentials credentials = new TokenCredentials(bat.getValue());
EnumSet<TraceFlags> es = EnumSet.of(TraceFlags.EwsRequest, TraceFlags.EwsRequestHttpHeaders, TraceFlags.EwsResponse, TraceFlags.EwsResponseHttpHeaders,TraceFlags.DebugMessage);

 service.setCredentials(credentials);
 service.setTraceEnabled(true);
 service.setTraceFlags(es);

FindItemsResults findResults = service.findItems(WellKnownFolderName.Inbox, itemview);

上面最后一步给出了错误。我对这个msal4j和EWS还是个新手。你能帮我解决这个问题吗?我需要访问Office 365邮箱,例如:abc@abc.biz

nuypyhwy

nuypyhwy1#

如果您使用服务主体身份验证(客户端凭据流),则还需要在EWS中使用模拟。因此,在您的代码中需要如下内容

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "abc@abc.biz");
brjng4g3

brjng4g32#

EWS请求:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><soap:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsa="http://www.w3.org/2005/08/addressing"><t:RequestServerVersion Version="Exchange2010"></t:RequestServerVersion><t:ExchangeImpersonation><t:ConnectingSID><t:SmtpAddress>lsrtestingmbx01@neustar.biz</t:SmtpAddress></t:ConnectingSID></t:ExchangeImpersonation>&lt;wsa:Action soap:mustUnderstand='1'&gt;http://schemas.microsoft.com/exchange/services/2006/messages/FindFolder&lt;/wsa:Action&gt;&lt;wsa:ReplyTo&gt;&lt;wsa:Address&gt;http://www.w3.org/2005/08/addressing/anonymous&lt;/wsa:Address&gt;&lt;/wsa:ReplyTo&gt;&lt;wsa:To soap:mustUnderstand='1'&gt;https://outlook.office365.com/EWS/Exchange.asmx/wssecurity&lt;/wsa:To&gt;&lt;wsse:Security soap:mustUnderstand='1'&gt;  nulleyJ0eXAiOiJKV1QiLCJub25jZSI6InUwc05rV2hLTjNrdXo0UzFCWWtKQzFwckxZYlZ6QWxFTHNXWEloSTBFSkkiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlMzY1LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzczYTJiYmMxLWYzMDctNDdjNC04Zjk0LTVmMzc5YzY4YmMzMC8iLCJpYXQiOjE2MjQ2MDQyMjYsIm5iZiI6MTYyNDYwNDIyNiwiZXhwIjoxNjI0N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c-jLGts_ZFDrgP4g&lt;/wsse:Security&gt;</soap:Header><soap:Body><m:FindFolder Traversal="Shallow"><m:FolderShape><t:BaseShape>AllProperties</t:BaseShape></m:FolderShape><m:IndexedPageFolderView MaxEntriesReturned="10" Offset="0" BasePoint="Beginning"></m:IndexedPageFolderView><m:ParentFolderIds><t:DistinguishedFolderId Id="inbox"></t:DistinguishedFolderId></m:ParentFolderIds></m:FindFolder></soap:Body></soap:Envelope>
kg7wmglp

kg7wmglp3#

我现在正在查看下面的HTTP标头:
跟踪标志:EwsResponseHttpHeaders值:401NULL X-Proxy-BackendServerStatus:401Server:microsoft-iis/10.0X-Proxy-RoutingGenness:1WWW-AUTIFICATE:承载client_id=“00000002-0000-0ff1-ce00-000000000000”,trusted_issuers=“00000001-0000-0000-c000-000000000000@*”,TOKEN_TYPE=“APP_ASSERTED_USER_v1 SERVICE_ASSERT_APP_V1”,authorization_uri=“https://login.windows.net/common/oauth2/authorize”,**ERROR=“INVALID_TOKEN”,基本领域=“”X-CalculatedBETarget:DM5PR1701MB1900.NAMPRD17.PROD.OUTLOOK.COM请求-id:e64c6048-3827-e102-de0b-8e25f54c83c4 X-RUM-VALIDATED:1 X-FEProxyInfo:DM5PR17CA0062.NAMPRD17.PROD.OUTLOOK.COM日期:星期一,2021年6月28日06:09:17格林威治标准时间x-ms诊断:2000008;原因=“令牌不包含任何权限,或无法理解权限。”;ERROR_CATEGORY=“INVALID_GRANT”**X-CalculatedFETarget:DM5PR17CU003.inter.outlook.com X-BE服务器:DM5PR1701MB1900 X-BackEndHttpStatus:401X-诊断信息:DM5PR1701MB1900 Set-Cookie:exchangecookie=4b4d794dbe6c41a89fba4e5b34fb9d66;Expires=Tue,28-Jun-2022 06:09:18 GMT;Path=/;Secure;HttpOnly X-FE服务器:BL0PR02CA0136-内容长度:0-Power-by:ASP.NET

wlp8pajw

wlp8pajw4#

而不是这样:

service.setCredentials(credentials);

试试这个:

service.getHttpHeaders().put("Authorization", "Bearer " + bat.getValue());

相关问题