如何从Microsoft Graph API获取正确订阅ID?与管理门户ID不一致
问题
客户拥有3个订阅:Exchange Online (Plan 1)、Microsoft 365 E3、Microsoft Teams Room Pro。在Microsoft 365管理门户中查看这些订阅时,点击Exchange Online Plan 1后,浏览器URL中assets后的ID为1f623e61-6645-4d63-d853-XXYZ。但调用Graph API的https://graph.microsoft.com/v1.0/directory/subscriptions端点返回的结果中,id字段与该ID不一致,commerceSubscriptionId也不匹配。请问是否调用了错误的端点,或是遗漏了什么?
调用示例代码:
$url ="https://graph.microsoft.com/v1.0/directory/subscriptions"; // 返回结果示例 stdClass Object ( [@odata.context] => https://graph.microsoft.com/v1.0/$metadata#directory/subscriptions [value] => Array ( [0] => stdClass Object ( [createdDateTime] => 2023-04-27T15:27:35Z [commerceSubscriptionId] => 1234567-de25-4aff-858c-AABBCCDD [id] => 123456-2a6d-4630-9931-SomethingNotCloseToOtherId [isTrial] => [nextLifecycleDateTime] => 2025-04-03T00:00:00Z [skuId] => c5928f49-12ba-48f7-ada3-0d743a3601d5 [skuPartNumber] => SomeExchangeSKu [status] => Enabled [totalLicenses] => 5 [ownerTenantId] => [ownerId] => [ownerType] => [serviceStatus] => Array ) ) )
解决方案
你调用的端点确实不对,具体说明如下:
directory/subscriptions的用途:这个端点返回的是Azure AD目录级别的订阅(比如Azure AD Premium P1/P2等),并非Microsoft 365的服务订阅(Exchange、E3这类许可证订阅)。- 正确的端点:要获取Microsoft 365管理门户中显示的订阅,应该调用
https://graph.microsoft.com/v1.0/subscribedSkus端点。 - 字段对应关系:
- 管理门户URL中
assets后的ID,对应subscribedSkus返回结果里的subscriptionId字段; skuPartNumber字段会显示订阅的标识(比如Exchange Online Plan 1对应的是EXCHANGESTANDARD,Microsoft 365 E3对应的是ENTERPRISEPACK);- 返回的
id字段格式为{租户ID}_{SKU ID},是租户内该SKU的唯一标识,和管理门户的URL ID不直接对应。
- 管理门户URL中
- 验证方式:调用
subscribedSkus后,你会看到包含subscriptionId的结果,其值会和管理门户URL中的assetsID完全匹配。
内容的提问来源于stack exchange,提问作者bart2puck
相关产品推荐
相关产品推荐

