我正在使用“仅应用程序”令牌检索我的用户,效果很好。
一旦应用程序在用户中循环,它应该创建一个订阅,如下所示。
但是,当尝试创建订阅时,请求只会返回:
代码:InvalidRequest消息:无法连接到远程服务器
内部错误
我的问题是,为什么订阅请求无法连接
当我显然能够在检索用户的第一个请求中成功连接时?
如何查看内部错误?
string tenantId = appSettings.TenantId;
var client = sdkHelper.GetAuthenticatedClientAppOnly(tenantId);
// this request works...
IGraphServiceUsersCollectionPage users = await client.Users.Request().Filter("userPrincipalName eq 'MY_USER_PRINCIPAL_NAME'").GetAsync();
if (users?.Count > 0)
{
foreach (User user in users)
{
// this request doesn't work...
Subscription newSubscription = new Subscription();
string clientState = Guid.NewGuid().ToString();
newSubscription = await client.Subscriptions.Request().AddAsync(new Subscription
{
Resource = $"users/{ user.Id }@{ tenantId }/mailFolders('Inbox')/messages",
//Resource = $"users/{ user.UserPrincipalName }/mailFolders('Inbox')/messages", // also tried using email address
ChangeType = "created",
NotificationUrl = "https://localhost:44334/notification/listen",
ClientState = clientState,
//ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 4230, 0) // current maximum lifespan for messages
ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 15, 0) // shorter duration useful for testing
});
}
}
当我将调用包装为try/catch时,错误消息就是这样,没有内部异常:
“Microsoft”类型的异常。图表引发了ServiceException“”。
我尝试了所有三个资源URL,但都产生了如上所示的相同错误:
-
资源=
$"users/{ user.Id }/mailFolders('Inbox')/messages",
-
资源=
$"users/{ user.Id }@{ tenantId }/mailFolders('Inbox')/messages",
-
资源=
$"users/{ user.UserPrincipalName }/mailFolders('Inbox')/messages",
我在github上找到了这个线程,但请求似乎对我不起作用。
-
允许仅应用程序请求创建订阅-
https://github.com/microsoftgraph/microsoft-graph-docs/issues/238