自动更新到Microsoft Graph API订阅

时间:2016-09-28 22:03:15

标签: asp.net-mvc asp.net-mvc-5 owin asp.net-webhooks

我使用Microsoft Graph API创建了webhook项目来监控Office 365收件箱。

我制作了一个UpdateSubscription操作方法,只根据https://graph.microsoft.io/en-us/docs/api-reference/v1.0/resources/subscription

上提供的文档提供了3天的续订时间

下面是我如何促进HTTP请求更新订阅的代码片段

        AuthenticationResult authResult = await AuthHelper.GetAccessTokenAsync();

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Build the request.
        string subscriptionsEndpoint = "https://graph.microsoft.com/v1.0/subscriptions/"+id;
        var method = new HttpMethod("PATCH");

        HttpRequestMessage request = new HttpRequestMessage(method, subscriptionsEndpoint);

        //get the current time

        var subscription = new Subscription
        {
            //Id = id,
            ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 4230, 0)
        };

有没有办法在没有用户按下按钮“更新”的情况下自动更新?

因为授权标头需要AuthResult.accessToken,这将要求用户登录Office365帐户。

请咨询

2 个答案:

答案 0 :(得分:1)

您可以使用的选项是服务或守护程序方法(https://graph.microsoft.io/en-us/docs/authorization/app_only)。您可以使用Bearer令牌在应用程序级别续订订阅,而不是使用登录用户进行身份验证,而令牌令牌又是由Azure AD的CLIENT_SECRET生成的。

我不认为在数据库中存储令牌是正确的方法。我的理解是安全令牌永远不适合数据库。

事实上,我不太明白需要让用户在这里登录,除非程序的某些部分没有提到。像我提到的那样的服务可以在没有用户的情况下监视邮箱,或者如果程序要求用户在那里,那么确实没有丢失凭证的问题。

答案 1 :(得分:0)

enter image description here您可以使用此方法使用grant_type密码从azure获取accesstoken。请找下面的截图。