分享方式:


透過合作夥伴中心 API 將授權指派給使用者

如何將授權指派給客戶使用者。

必要條件

  • 認證,如合作夥伴中心驗證所述。 此案例僅支援使用「應用程式+使用者」認證來進行驗證。

  • 客戶用戶識別碼。 此標識碼會識別要指派授權的使用者。

  • 識別授權產品的產品 SKU 識別碼。

GDAP 角色

您至少需要下列其中一個 GDAP 角色:

  • 授權管理員
  • 使用者管理員
  • 目錄寫入者

透過程式代碼指派授權

當您將授權指派給使用者時,您必須從客戶的已訂閱 SKU 集合中選擇。 然後,識別出您想要指派的產品之後,您必須取得每個產品的產品 SKU 標識符,才能進行指派。 每個 SubscribedSku 實例都包含 ProductSku 屬性,您可以從中參考 ProductSku 物件並取得標識碼。

以下是透過程式代碼指派授權的步驟:

  1. 具現化 LicenseAssignment 物件。 您可以使用這個物件來指定要指派的產品 SKU 和服務方案。

    LicenseAssignment license = new LicenseAssignment();
    
  2. 填入物件屬性,如下所示。 此程式代碼假設您已經擁有產品 SKU 標識符,而且會指派所有可用的服務方案(也就是說,不會排除任何方案)。

    license.SkuId = selectedProductSkuId;
    license.ExcludedPlans = null;
    
  3. 如果您沒有產品 SKU 標識符,您必須擷取已訂閱 SKU 的集合,並從其中一個 SKU 取得產品 SKU 標識符。 如果您知道產品 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. 呼叫 Create 或 CreateAsync 方法,並傳遞授權更新物件,如下所示來指派授權。

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

C#

若要將授權指派給客戶使用者,請先具現化 LicenseAssignment 對象,然後填入 SkuidExcludedPlans 屬性。 您可以使用這個物件來指定要指派的產品 SKU,以及要排除的服務方案。 接下來,具現化 LicenseAssignment 類型的新清單,並將授權物件新增至清單。 然後建立 LicenseUpdate 實例,並將授權指派清單指派給 LicensesToAssign 屬性。

接下來,使用 IAggregatePartner.Customers.ById 方法搭配客戶標識碼來識別客戶,並使用 User.ById 方法來識別使用者。 然後,從 License 更新 屬性取得客戶使用者授權更新作業的介面。

最後,呼叫 Create 或 CreateAsync 方法,並傳遞授權更新物件來指派授權。

// 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 字串 Yes 識別客戶的 GUID 格式識別碼。
user-id 字串 Yes 識別使用者的 GUID 格式識別碼。

要求標頭

如需詳細資訊,請參閱合作夥伴中心 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"
}