从Office365中的Management Activity API获取缺少的审核日志

时间:2016-10-28 13:15:51

标签: api office365 office365api

我们的应用程序调用开箱即用的Office 365 Management API来检索存储在SharePoint Online中的文件的活动和事件。但是根据我们的实验,应用程序似乎无法检索到不够的日志。

示例:我们将1000个文件上传到Sharepoint Online中的文档库。我们收到8个订阅。每次订阅,我们最多只能获得100个日志。总调用API获取日志以检索600个日志。还不够!

这是我获得订阅的代码

List<SubscriptionsContent> GetSubscriptionsContents(AuthenticationResult authenticationResult, ManagementAPI m, DateTime startDate, DateTime endDate, bool proxyRequired = false)
    {
        try
        {
            string jsonSubscription = string.Empty;
            string url = string.Empty;
            string logType = "Audit.SharePoint";

            if (authenticationResult != null)
            {
                url = string.Format(UrlFormat, m.TenantId, string.Format("subscriptions/content?contentType={0}&startTime={1}&endTime={2}", logType, startDate.ToUniversalTime().ToString(DateFormat), endDate.ToUniversalTime().ToString(DateFormat)));
                jsonSubscription = ExecuteRequest(url, HttpMethod.Get, authenticationResult);
                //Log.Info("jsonSubscription:");
                //Log.Info(jsonSubscription);
            }
            var listContent = Common.GetListSubscriptionsContent(jsonSubscription);
            Log.Info("Common.GetListSubscriptionsContent(jsonSubscription); Count: " + (listContent != null ? listContent.Count.ToString() : "IS NULL"));
            return listContent;
        }
        catch (Exception ex)
        {
            Log.Error(ex);
            return new List<SubscriptionsContent>();
        }
    }

这是我执行请求的代码

public string ExecuteRequest(string url, HttpMethod method, AuthenticationResult token)
    {
        var responseStr = "";
        try
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpRequestMessage request = new HttpRequestMessage(method, url);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
            HttpResponseMessage response = client.SendAsync(request).Result;
            Log.Info("ExecuteRequest(string url, HttpMethod method, AuthenticationResult token): response.StatusCode: " + response.StatusCode + " ; response.ReasonPhrase: " + response.ReasonPhrase + " ; response.RequestMessage: " + response.RequestMessage);


            if (response.IsSuccessStatusCode)
            {
                responseStr = response.Content.ReadAsStringAsync().Result;
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex);
        }

        return responseStr;
    }

这是我从每个订阅中获取审核日志的代码

List<AuditLog> listAudit = new List<AuditLog>();
                foreach (var item in listSubscription)
                {
                    var jsonAudit = ExecuteRequest(item.ContentUri.ToString(), HttpMethod.Get, authenticationResult);

                    if (string.IsNullOrEmpty(jsonAudit))
                        continue;
                    var listAuditLog = Common.GetListAuditLog(jsonAudit);
                  }

这里我的代码解析器JsonString

public static List<AuditLog> GetListAuditLog(string jsonString)
    {
        try
        {
            return JsonConvert.DeserializeObject<List<AuditLog>>(jsonString);
        }
        catch (Exception ex)
        {
            Log.Error("public static List<AuditLog> GetListAuditLog(string jsonString)", ex.InnerException);
            return new List<AuditLog>();
        }
    }

1 个答案:

答案 0 :(得分:2)

我认为您需要使用分页标题。

如果数据量太大,API将返回名为NextPageUrl的标头条目,其中包含用于请求下一页结果的地址。此链接(代表查询)将在24小时内可用。

实施例

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
NextPageUrl:https://manage.office.com/api/v1/{tenant_id}/activity/feed/subscriptions/content?contentType=Audit.SharePoint&amp;startTime=2015-10-01&amp;endTime=2015-10-02&amp;nextPage=2015101900R022885001761

因此,如果响应包含此标头条目,只需使用NextPageUrl的值来请求更多数据。

重复此过程,直到此标题条目不再存在。

您可以在Office 365 Management API reference

中找到更多信息