You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python获取Office 365许可证Sku ID并分配给用户

获取Office 365许可证SKU ID的方法

你可以通过以下两种方式获取租户内的许可证SKU ID:

一、使用Microsoft Graph API调用

利用你已通过MSAL获取的访问令牌,调用GET /subscribedSkus接口即可返回所有已订阅的许可证信息,其中包含所需的skuId字段。

示例Python调用代码

在现有MSAL令牌获取代码基础上,添加以下请求逻辑:

import requests

# 保留你原有的MSAL令牌获取代码...
access_token = result['access_token']

graph_url = "https://graph.microsoft.com/v1.0/subscribedSkus"
headers = {
    "Authorization": f"Bearer {access_token}"
}

response = requests.get(graph_url, headers=headers)
if response.status_code == 200:
    skus = response.json()['value']
    for sku in skus:
        print(f"许可证名称: {sku['skuPartNumber']}, SKU ID: {sku['skuId']}")
else:
    print(f"请求失败: {response.text}")

返回结果中,skuPartNumber是许可证的标识名称(如ENTERPRISEPACK对应Office 365 E3),skuId就是你需要的许可证ID。

二、使用PowerShell获取

方法1:Microsoft Graph PowerShell模块

先安装并连接模块:

Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Directory.Read.All"

执行命令获取SKU信息:

Get-MgSubscribedSku | Select-Object SkuPartNumber, SkuId

方法2:Azure AD PowerShell模块(旧版)

如果使用旧模块,执行以下命令:

Install-Module AzureAD -Scope CurrentUser
Connect-AzureAD
Get-AzureADSubscribedSku | Select-Object SkuPartNumber, SkuId

许可证分配注意事项

获取SKU ID后,可调用POST /users/{用户ID或用户主体名}/assignLicense接口为用户分配许可证,请求体需包含addLicenses(指定SKU ID和要启用的服务计划)和removeLicenses(留空则不移除现有许可证)。

需确保你的应用注册已添加以下需管理员同意的权限:

  • Directory.Read.All(用于读取许可证SKU)
  • User.ReadWrite.All、Directory.ReadWrite.All(用于分配许可证)

内容的提问来源于stack exchange,提问作者Shiv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 02:10:04