代码之家  ›  专栏  ›  技术社区  ›  user1477388

仅应用程序令牌获取用户,但无法创建订阅?

  •  -1
  • user1477388  · 技术社区  · 6 年前

    我正在使用“仅应用程序”令牌检索我的用户,效果很好。

    一旦应用程序在用户中循环,它应该创建一个订阅,如下所示。

    但是,当尝试创建订阅时,请求只会返回:

    代码: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上找到了这个线程,但请求似乎对我不起作用。

    1. 允许仅应用程序请求创建订阅- https://github.com/microsoftgraph/microsoft-graph-docs/issues/238
    1 回复  |  直到 6 年前
        1
  •  1
  •   user1477388    6 年前

    NotificationUrl不能为“ 本地服务器 哦!“请求是从远程服务器发送的,因此它不知道本地主机是什么,因此它会失败。如果我将项目部署到远程服务器并传递URL,它可能会工作。我将尝试。。。