通过合作伙伴中心 API 向用户分配许可证

如何向客户用户分配许可证。

先决条件

  • 合作伙伴中心身份验证中所述的凭据。 此方案只支持使用应用凭据和用户凭据进行身份验证。

  • 客户用户标识符。 此 ID 标识要向其分配许可证的用户。

  • 标识许可证产品的产品 SKU 标识符。

GDAP 角色

至少需要以下 GDAP 角色之一:

  • 许可证管理员
  • 用户管理员
  • 目录编写人员

通过代码分配许可证

向用户分配许可证时,必须从客户的订阅 SKU 集合中进行选择。 然后,确定要分配的产品后,必须获取每个产品的产品 SKU ID 才能进行分配。 每个 SubscribedSku 实例都包含一个 ProductSku 属性,可以从中引用 ProductSku 对象并获取 ID

下面是通过代码分配许可证的步骤:

  1. 实例化 LicenseAssignment 对象。 使用此对象指定要分配的产品 SKU 和服务计划。

    LicenseAssignment license = new LicenseAssignment();
    
  2. 填充对象属性,如下所示。 此代码假定你已有产品 SKU ID,并且将分配所有可用的服务计划(即,不会排除任何计划)。

    license.SkuId = selectedProductSkuId;
    license.ExcludedPlans = null;
    
  3. 如果没有产品 SKU ID,则需要检索已订阅 SKU 的集合,并从其中一个 SKU 获取产品 SKU ID。 如果知道产品 SKU 名称,下面是一个示例。

    var customerSubscribedSkus = partnerOperations.Customers.ById(selectedCustomerId).SubscribedSkus.Get();
    var sku = customerSubscribedSkus.Items.Where(n => n.ProductSku.Name == "Office 365 Enterprise E3").First();
    license.SkuId = sku.ProductSku.Id;
    license.ExcludedPlans = null;
    
  4. 接下来,实例化 LicenseAssignment 类型的新列表,并添加许可证对象。 可以通过将每个许可证单独添加到列表中来分配多个许可证。 此列表中包括的许可证必须来自同一许可证组。

    List<LicenseAssignment> licenseList = new List<LicenseAssignment>();
    licenseList.Add(license);
    
  5. 创建 LicenseUpdate 实例,并将许可证分配列表分配给 LicensesToAssign 属性。

    LicenseUpdate updateLicense = new LicenseUpdate();
    updateLicense.LicensesToAssign = licenseList;
    
  6. 调用 CreateCreateAsync 方法并传递许可证更新对象,如下所示来分配许可证。

    var assignLicense = partnerOperations.Customers.ById(selectedCustomerId).Users.ById(selectedCustomerUserId).LicenseUpdates.Create(updateLicense);
    

C#

若要向客户用户分配许可证,请先实例化 LicenseAssignment 对象,并填充 SkuidExcludedPlans 属性。 使用此对象指定要分配的产品 SKU 以及要排除的服务计划。 接下来,实例化 LicenseAssignment 类型的新列表,并将许可证对象添加到列表中。 然后创建 LicenseUpdate 实例,并将许可证分配列表分配给 LicensesToAssign 属性。

接下来,使用具有客户 ID 的 IAggregatePartner.Customers.ById 方法来标识客户,使用用户 ID 的 Users.ById 方法标识用户。 然后,从许可证汇报属性获取客户用户许可证更新操作的接口。

最后,调用 CreateCreateAsync 方法并传递许可证更新对象来分配许可证。

// IAggregatePartner partnerOperations;
// string selectedCustomerUserId;
// string selectedCustomerId;
// string selectedProductSkuId;

// Instantiate and populate a LicenseAssignment object.
LicenseAssignment license = new LicenseAssignment();
license.SkuId = selectedProductSkuId;
license.ExcludedPlans = null;

// Instantiate a list of licenses to assign and add the license to it.
List<LicenseAssignment> licenseList = new List<LicenseAssignment>();
licenseList.Add(license);

// Instantiate a LicenseUpdate object and add the list of licenses to assign.
LicenseUpdate updateLicense = new LicenseUpdate();
updateLicense.LicensesToAssign = licenseList;

// Update the user licenses.
var assignLicense = partnerOperations.Customers.ById(selectedCustomerId).Users.ById(selectedCustomerUserId).LicenseUpdates.Create(updateLicense);

示例控制台测试应用项目:合作伙伴中心 SDK 示例 :CustomerUserAssignLicenses.cs

REST 请求

请求语法

方法 请求 URI
POST {baseURL}/v1/customers/{customer-id}/users/{user-id}/licenseupdates HTTP/1.1

URI 参数

使用以下路径参数标识客户和用户。

名称 类型​​ 必需 说明
customer-id string 标识客户的 GUID 格式 ID。
user-id string 标识用户的 GUID 格式 ID。

请求标头

有关详细信息,请参阅合作伙伴中心 REST 标头

请求正文

在请求正文中包含一个 LicenseUpdate 资源,该资源指定要分配的许可证。

请求示例

POST https://api.partnercenter.microsoft.com/v1/customers/0c39d6d5-c70d-4c55-bc02-f620844f3fd1/users/554526aa-cf5e-46fa-95df-98dbc55d8a1e/licenseupdates HTTP/1.1
Authorization: Bearer <token>
Accept: application/json
MS-RequestId: a37d3009-665d-4e12-b76e-1aa10cf80140
MS-CorrelationId: c73c9570-c352-459e-98a3-dafe4bd8c821
X-Locale: en-US
MS-PartnerCenter-Client: Partner Center .NET SDK
Content-Type: application/json
Host: api.partnercenter.microsoft.com
Content-Length: 183
Expect: 100-continue

{
    "LicensesToAssign": [{
            "ExcludedPlans": null,
            "SkuId": "f8a1db68-be16-40ed-86d5-cb42ce701560"
        }
    ],
    "LicensesToRemove": null,
    "LicenseWarnings": null,
    "Attributes": {
        "ObjectType": "LicenseUpdate"
    }
}

重要

截至 2023 年 6 月,最新的合作伙伴中心 .NET SDK 版本 3.4.0 现已存档。 可以从 GitHub 下载 SDK 版本,以及包含有用信息的自述文件

建议合作伙伴继续使用合作伙伴中心 REST API

REST 响应

如果成功,则返回 HTTP 响应状态代码 201,响应正文包含具有 许可证信息的 LicenseUpdate 资源。

响应的成功和错误代码

每个响应都带有一个 HTTP 状态代码,用于指示成功或失败以及其他调试信息。 请使用网络跟踪工具来读取此代码、错误类型和其他参数。 如需完整列表,请参阅合作伙伴中心 REST 错误代码

响应示例(成功)

HTTP/1.1 201 Created
Content-Length: 139
Content-Type: application/json; charset=utf-8
MS-CorrelationId: c73c9570-c352-459e-98a3-dafe4bd8c821
MS-RequestId: a37d3009-665d-4e12-b76e-1aa10cf80140
MS-CV: 5AnzcZQrvUqCq3kd.0
MS-ServerId: 030020525
Date: Thu, 20 Apr 2017 21:50:39 GMT

{
    "licensesToAssign": [{
            "skuId": "f8a1db68-be16-40ed-86d5-cb42ce701560"
        }
    ],
    "licenseWarnings": [],
    "attributes": {
        "objectType": "LicenseUpdate"
    }
}

响应示例(许可证不可用)

HTTP/1.1 400 Bad Request
Content-Length: 341
Content-Type: application/json; charset=utf-8
MS-CorrelationId: c73c9570-c352-459e-98a3-dafe4bd8c821
MS-RequestId: f4f3b748-8b22-4d07-a5a1-dceb32824192
MS-CV: 5npA0K22CUmWPOzB.0
MS-ServerId: 102030524
Date: Thu, 20 Apr 2017 22:12:36 GMT

{
    "code": 60012,
    "description": "We are sorry, it looks like you have run out of licenses. Buy more licenses, and then try again.",
    "data": ["LicenseQuotaExceededException : Subscription with Account 0c39d6d5-c70d-4c55-bc02-f620844f3fd1 and SKU f8a1db68-be16-40ed-86d5-cb42ce701560 does not have any available licenses left."],
    "source": "PartnerFD"
}

响应示例(无效令牌)

HTTP/1.1 400 Bad Request
Content-Length: 342
Content-Type: application/json; charset=utf-8
MS-CorrelationId: c5359db2-fb21-485c-90b1-0a2c3b484783
MS-RequestId: f0caf048-380c-4d6f-968a-3ccce01e11c1
MS-CV: 6npA0K22CUmWPOzC.0
MS-ServerId: 102045524
Date: Thu, 20 Apr 2023 21:12:36 GMT

{
    "code": 900316,
    "description": "Partner token is not allowed in license management calls. Please call with Partner Access Token. To learn more, see https://learn.microsoft.com/partner-center/developer/enable-secure-app-model#get-access-token",
    "source": "PartnerFD"
}

响应示例(条件访问策略问题)

HTTP/1.1 403 Forbidden
Content-Length: 342
Content-Type: application/json; charset=utf-8
MS-CorrelationId: c5359db2-fb21-485c-90b1-0a2c3b484783
MS-RequestId: f0caf048-380c-4d6f-968a-3ccce01e11c1
MS-CV: 6npA0K22CUmWPOzC.0
MS-ServerId: 102045524
Date: Thu, 20 Apr 2023 21:12:36 GMT

{
    "code": 900309,
    "description": "Access has been blocked by Conditional Access policies. The access policy does not allow token issuance. Learn more at https://learn.microsoft.com/azure/active-directory/conditional-access/concept-conditional-access-users-groups.",
    "source": "PartnerFD"
}