400错误访问Microsoft Graph的ExchangeService时发生。

huangapple go评论62阅读模式
英文:

400 error while accessing ExchangeService through Microsoft Graph

问题

我面临一个奇怪的问题,无法找出解决方法。对此有任何建议/解决方案将不胜感激。提前致谢。

背景: 请求失败。远程服务器返回错误: (400) 错误的请求。
C# 应用程序在调用 GetRoomLists 方法时出现上述消息的错误,而 ExchangeService 的实例/对象正在调用该方法。

详情: 目标是获取与特定用户关联的房间列表,该用户属于公司的 Exchange 服务器。为了实现这一目标,编写了以下代码片段:

var cca = ConfidentialClientApplicationBuilder
            .Create("*******")
            .WithClientSecret("*******")
            .WithTenantId("******")
            .Build();

var ewsScopes2 = new string[] { "https://graph.microsoft.com/.default" };
             
var authResult2 = cca.AcquireTokenForClient(ewsScopes2).ExecuteAsync().Result;

// 使用访问令牌配置 ExchangeService

var _service = new ExchangeService();
_service.Url = new Uri("https://graph.microsoft.com/v1.0/me/calendar/events");

_service.Credentials = new OAuthCredentials(authResult2.AccessToken);

_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "abc@mycompany.onmicrosoft.com");
}

EmailAddressCollection roomList = _service.GetRoomLists();

在尝试访问 GetRoomLists 时,我们收到错误消息:“请求失败。远程服务器返回错误: (400) 错误的请求。”

AAD 配置:
在 Azure AD 中,Graph 对象已被授予对 Calendars.Room 资源的委托权限。

英文:

I am facing a wired problem and not able to figure out the solution. Any suggestions/solutions on this will be much appreciated. Thanks in advance.
Context: The request failed. The remote server returned an error: (400) Bad Request.
C# App is throwing an error with the above message while instance/object of the ExchangeService is Calling GetRoomLists method.

Details: The aim is to get the list of Rooms associated with specific user belongs to a corporate Exchange Server. To achieve the purpose, following code snippets are written

    var cca = ConfidentialClientApplicationBuilder
                        .Create("*******")
                        .WithClientSecret("*******")
                        .WithTenantId("******")
                        .Build();

   
 

    var ewsScopes2 = new string[] { "https://graph.microsoft.com/.default" };
             
    var authResult2 = cca.AcquireTokenForClient(ewsScopes2).ExecuteAsync().Result;


// Configure the ExchangeService with the access token

    var _service = new ExchangeService();
                        _service.Url = new Uri("https://graph.microsoft.com/v1.0/me/calendar/events");

                   
                

_service.Credentials = new OAuthCredentials(authResult2.AccessToken);
               

 _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "abc@mycompany.onmicrosoft.com");
            }
                   
    EmailAddressCollection roomList = _service.GetRoomLists();

While trying to access the GetRoomLists, we are getting the error “The request failed. The remote server returned an error: (400) Bad Request.”

AAD Configuration:
In Azure AD the Graph object has delegated permission to Calendars.Room resource.

答案1

得分: 0

// 用访问令牌配置 ExchangeService
var _service = new ExchangeService();
_service.Url = new Uri("https://graph.microsoft.com/v1.0/me/calendar/events");
_service.Credentials = new OAuthCredentials(authResult2.AccessToken);
_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "abc@mycompany.onmicrosoft.com");
}
EmailAddressCollection roomList = _service.GetRoomLists();

这是所有的 Exchange Web Services (EWS,基于 SOAP 的 API) 代码,所以不适用于 Graph (基于 REST)。等效的方法是使用 Graph SDK https://learn.microsoft.com/en-us/graph/sdks/sdks-overview,然后使用 Places 端点。要使用此端点,您需要 Place.Read.All 应用程序权限。

https://learn.microsoft.com/en-us/graph/api/place-list?view=graph-rest-1.0&tabs=csharp

请注意,您可能需要注意关于 SDK 的 v5 版本的这个问题 https://github.com/microsoftgraph/microsoft-graph-docs/issues/21503,因为这会影响您要做的事情。

英文:
 // Configure the ExchangeService with the access token
 var _service = new ExchangeService();
                    _service.Url = new 
Uri("https://graph.microsoft.com/v1.0/me/calendar/events");            
_service.Credentials = new OAuthCredentials(authResult2.AccessToken);
_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "abc@mycompany.onmicrosoft.com");
        }
EmailAddressCollection roomList = _service.GetRoomLists();

This is all Exchange Web Services (EWS which is a SOAP based API) code so won't work against the Graph (which is REST based). The equivalent would be to use the Graph SDK https://learn.microsoft.com/en-us/graph/sdks/sdks-overview and the use the Places Endpoint. To use this endpoint you will need to the Place.Read.All Application permission.

https://learn.microsoft.com/en-us/graph/api/place-list?view=graph-rest-1.0&tabs=csharp

Note you might need to be aware of this issue with the v5 version of the SDK https://github.com/microsoftgraph/microsoft-graph-docs/issues/21503 as that will affect what your tying to do.

huangapple
  • 本文由 发表于 2023年6月27日 21:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定