获取合作伙伴中心活动的记录
适用于:合作伙伴中心 | Microsoft Cloud for US Government 合作伙伴中心
本文介绍如何检索合作伙伴用户或应用程序在一段时间内执行的操作记录。
使用此 API 可检索过去 30 天的审核记录,或检索指定日期范围(包括开始日期和/或结束日期) 的日期范围。 但是,请注意,出于性能原因,活动日志数据可用性限制为前 90 天。 在当前日期之前的开始日期大于 90 天的请求将收到错误请求异常(错误代码:400)和相应的消息。
先决条件
- 合作伙伴中心身份验证中所述的凭据。 此方案支持使用独立应用和 App+User 凭据进行身份验证。
C#
若要检索合作伙伴中心操作的记录,请先为要检索的记录建立日期范围。 下面的代码示例仅使用开始日期,但也可以包含结束日期。 有关详细信息,请参阅 Query 方法。 接下来,为要应用的筛选器类型创建所需的变量,并分配相应的值。 例如,若要按公司名称子字符串进行筛选,请创建一个用于保存子字符串的变量。 若要按客户 ID 进行筛选,请创建一个用于保存 ID 的变量。
在以下示例中,提供了示例代码,以便按公司名称子字符串、客户 ID 或资源类型进行筛选。 选择一个并注释掉另一个。 在每个情况下,首先使用其默认构造函数实例化 SimpleFieldFilter 对象来创建筛选器。 需要传递包含要搜索的字段的字符串,以及要应用的相应运算符,如下所示。 还必须提供要筛选依据的字符串。
接下来,使用 AuditRecords 属性获取用于审核记录操作的接口,并调用 Query 或 QueryAsync 方法来执行筛选器并获取表示结果第一页的 AuditRecord 的集合。 传递方法开始日期、此处示例中未使用的可选结束日期,以及 表示实体查询的 IQuery 对象。 IQuery 对象是通过将上面创建的筛选器传递给 QueryFactory 的 BuildSimpleQuery 方法创建的。
获得项的初始页后,使用 Enumerators.AuditRecords.Create 方法创建可用于循环访问剩余页面的枚举器。
// IAggregatePartner partnerOperations;
var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01);
// First perform the query, then get the enumerator. Choose one of the following and comment out the other two.
// To retrieve audit records by company name substring (for example "bri" matches "Fabrikam, Inc.").
var searchSubstring="bri";
var filter = new SimpleFieldFilter(AuditRecordSearchField.CompanyName.ToString(), FieldFilterOperation.Substring, searchSubstring);
var auditRecordsPage = partnerOperations.AuditRecords.Query(startDate.Date, query: QueryFactory.Instance.BuildSimpleQuery(filter));
// To retrieve audit records by customer ID.
var customerId="0c39d6d5-c70d-4c55-bc02-f620844f3fd1";
var filter = new SimpleFieldFilter(AuditRecordSearchField.CustomerId.ToString(), FieldFilterOperation.Equals, customerId);
var auditRecordsPage = partnerOperations.AuditRecords.Query(startDate.Date, query: QueryFactory.Instance.BuildSimpleQuery(filter));
// To retrieve audit records by resource type.
int resourceTypeInt = 3; // Subscription Resource.
string searchField = Enum.GetName(typeof(ResourceType), resourceTypeInt);
var filter = new SimpleFieldFilter(AuditRecordSearchField.ResourceType.ToString(), FieldFilterOperation.Equals, searchField);
var auditRecordsPage = partnerOperations.AuditRecords.Query(startDate.Date, query: QueryFactory.Instance.BuildSimpleQuery(filter));
var auditRecordEnumerator = partnerOperations.Enumerators.AuditRecords.Create(auditRecordsPage);
int pageNumber = 1;
while (auditRecordEnumerator.HasValue)
{
// Work with the current page.
foreach (var c in auditRecordEnumerator.Current.Items)
{
// Display some info, such as operation type, operation date, and operation status.
Console.WriteLine(string.Format("{0} {1} {2}.", c.OperationType, c.OperationDate, c.OperationStatus));
}
// Get the next page of audit records.
auditRecordEnumerator.Next();
}
示例: 控制台测试应用。 项目:合作伙伴中心 SDK 示例 文件夹:审核
REST 请求
请求语法
方法 | 请求 URI |
---|---|
GET | {baseURL}/v1/auditrecords?startDate={startDate} HTTP/1.1 |
GET | {baseURL}/v1/auditrecords?startDate={startDate}&endDate={endDate} HTTP/1.1 |
GET | {baseURL}/v1/auditrecords?startDate={startDate}&endDate={endDate}&filter={“Field”:“CompanyName”,“Value”:“{searchSubstring}”,“Operator”:“substring”} HTTP/1.1 |
GET | {baseURL}/v1/auditrecords?startDate={startDate}&endDate={endDate}&filter={“Field”:“CustomerId”,“Value”:“{customerId}”,“Operator”:“equals”} HTTP/1.1 |
GET | {baseURL}/v1/auditrecords?startDate={startDate}&endDate={endDate}&filter={“Field”:“ResourceType”,“Value”:“{resourceType}”,“Operator”:“equals”} HTTP/1.1 |
URI 参数
创建请求时,请使用以下查询参数。
名称 | 类型 | 必需 | 说明 |
---|---|---|---|
startDate | date | 否 | yyyy-mm-dd 格式的开始日期。 如果未提供,则结果集默认为请求日期之前的 30 天。 提供筛选器时,此参数是可选的。 |
endDate | date | 否 | yyyy-mm-dd 格式的结束日期。 提供筛选器时,此参数是可选的。 当省略结束日期或设置为 null 时,请求将返回最大窗口或今天用作结束日期,以较小者为准。 |
filter | string | 否 | 要应用的筛选器。 此参数必须是编码的字符串。 提供开始日期或结束日期时,此参数是可选的。 |
筛选器语法
必须将筛选器参数组合为一系列逗号分隔的键值对。 每个键和值必须单独引用,并用冒号隔开。 整个筛选器必须编码。
未编码的示例如下所示:
?filter{"Field":"CompanyName","Value":"bri","Operator":"substring"}
下表描述了所需的键值对:
密钥 | 值 |
---|---|
字段 | 要筛选的字段。 可以在 Request 语法中找到支持的值。 |
值 | 要按其进行筛选的值。 忽略值大小写。 请求语法中显示了以下值参数: searchSubstring - 替换为公司的名称。 可以输入子字符串以匹配公司名称的一部分(例如, bri 匹配 Fabrikam, Inc )。示例: "Value":"bri" customerId - 替换为表示客户标识符的 GUID 格式字符串。 示例: "Value":"0c39d6d5-c70d-4c55-bc02-f620844f3fd1" resourceType - 替换为要检索其审核记录的资源类型(例如订阅)。 可用资源类型在 ResourceType 中定义。 示例: "Value":"Subscription" |
操作员 | 要应用的运算符。 可以在 Request 语法中找到支持的运算符。 |
请求标头
- 有关详细信息,请参阅 Parter Center REST 标头。
请求正文
无。
请求示例
GET https://api.partnercenter.microsoft.com/v1/auditrecords?startDate=6/1/2017%2012:00:00%20AM&filter=%7B%22Field%22:%22CustomerId%22,%22Value%22:%220c39d6d5-c70d-4c55-bc02-f620844f3fd1%22,%22Operator%22:%22equals%22%7D HTTP/1.1
Authorization: Bearer <token>
Accept: application/json
MS-RequestId: 127facaa-e389-41f8-8bb7-1d1af99db893
MS-CorrelationId: de9c2ccc-40dd-4186-9660-65b9b64c3d14
X-Locale: en-US
Host: api.partnercenter.microsoft.com
Connection: Keep-Alive
REST 响应
如果成功,此方法将返回一组符合筛选器的活动。
响应的成功和错误代码
每个响应都带有一个 HTTP 状态代码,用于指示成功或失败以及其他调试信息。 请使用网络跟踪工具来读取此代码、错误类型和其他参数。 如需完整列表,请参阅合作伙伴中心 REST 错误代码。
响应示例
HTTP/1.1 200 OK
Content-Length: 2859
Content-Type: application/json; charset=utf-8
MS-CorrelationId: de9c2ccc-40dd-4186-9660-65b9b64c3d14
MS-RequestId: 127facaa-e389-41f8-8bb7-1d1af99db893
MS-CV: 4xDKynq/zE2im0wj.0
MS-ServerId: 030011719
Date: Tue, 27 Jun 2017 22:19:46 GMT
{
"totalCount": 2,
"items": [{
"partnerId": "3b33e682-00c3-41ee-9dd2-a548adf56438",
"customerId": "0c39d6d5-c70d-4c55-bc02-f620844f3fd1",
"customerName": "Relecloud",
"userPrincipalName": "admin@domain.onmicrosoft.com",
"resourceType": "order",
"resourceNewValue": "{\"Id\":\"d51a052e-043c-4a2a-aa37-2bb938cef6c1\",\"ReferenceCustomerId\":\"0c39d6d5-c70d-4c55-bc02-f620844f3fd1\",\"BillingCycle\":\"none\",\"LineItems\":[{\"LineItemNumber\":0,\"OfferId\":\"C0BD2E08-11AC-4836-BDC7-3712E744922F\",\"SubscriptionId\":\"488745B5-2086-4912-802C-6ABB9F7C3638\",\"ParentSubscriptionId\":null,\"FriendlyName\":\"Office 365 Business Premium Trial\",\"Quantity\":25,\"PartnerIdOnRecord\":null,\"Links\":{\"Subscription\":{\"Uri\":\"/customers/0c39d6d5-c70d-4c55-bc02-f620844f3fd1/subscriptions/488745B5-2086-4912-802C-6ABB9F7C3638\",\"Method\":\"GET\",\"Headers\":[]}}}],\"CreationDate\":\"2017-06-15T15:56:04.077-07:00\",\"Links\":{\"Self\":{\"Uri\":\"/customers/0c39d6d5-c70d-4c55-bc02-f620844f3fd1/orders/d51a052e-043c-4a2a-aa37-2bb938cef6c1\",\"Method\":\"GET\",\"Headers\":[]}},\"Attributes\":{\"Etag\":\"eyJpZCI6ImQ1MWEwNTJlLTA0M2MtNGEyYS1hYTM3LTJiYjkzOGNlZjZjMSIsInZlcnNpb24iOjF9\",\"ObjectType\":\"Order\"}}",
"operationType": "create_order",
"operationDate": "2017-06-15T22:56:05.0589308Z",
"operationStatus": "succeeded",
"customizedData": [{
"key": "OrderId",
"value": "d51a052e-043c-4a2a-aa37-2bb938cef6c1"
}, {
"key": "BillingCycle",
"value": "None"
}, {
"key": "OfferId-0",
"value": "C0BD2E08-11AC-4836-BDC7-3712E744922F"
}, {
"key": "SubscriptionId-0",
"value": "488745B5-2086-4912-802C-6ABB9F7C3638"
}, {
"key": "SubscriptionName-0",
"value": "Office 365 Business Premium Trial"
}, {
"key": "Quantity-0",
"value": "25"
}, {
"key": "PartnerOnRecord-0",
"value": null
}
],
"attributes": {
"objectType": "AuditRecord"
}
}, {
"partnerId": "3b33e682-00c3-41ee-9dd2-a548adf56438",
"customerId": "0c39d6d5-c70d-4c55-bc02-f620844f3fd1",
"customerName": "Relecloud",
"userPrincipalName": "admin@domain.onmicrosoft.com",
"applicationId": "Partner Center Native App",
"resourceType": "license",
"resourceNewValue": "{\"LicensesToAssign\":[{\"ExcludedPlans\":null,\"SkuId\":\"efccb6f7-5641-4e0e-bd10-b4976e1bf68e\"}],\"LicensesToRemove\":null,\"LicenseWarnings\":[],\"Attributes\":{\"ObjectType\":\"LicenseUpdate\"}}",
"operationType": "update_customer_user_licenses",
"operationDate": "2017-06-01T20:09:07.0450483Z",
"operationStatus": "succeeded",
"customizedData": [{
"key": "CustomerUserId",
"value": "482e2152-4b49-48ec-b715-823365ce3d4c"
}, {
"key": "AddedLicenseSkuId",
"value": "efccb6f7-5641-4e0e-bd10-b4976e1bf68e"
}
],
"attributes": {
"objectType": "AuditRecord"
}
}
],
"links": {
"self": {
"uri": "/auditrecords?startDate=2017-06-01&size=500&filter=%7B%22Field%22%3A%22CustomerId%22%2C%22Value%22%3A%220c39d6d5-c70d-4c55-bc02-f620844f3fd1%22%2C%22Operator%22%3A%22equals%22%7D",
"method": "GET",
"headers": []
}
},
"attributes": {
"objectType": "Collection"
}
}