如何获取Office 365许可证Python的Sku Id值

问题描述 投票:0回答:1

通过在 Python 中使用 msal 创建令牌,我尝试向用户分配许可证。

import msal

client_id = xxx
client_secret= xxx
tenant_id = xxx
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ['https://graph.microsoft.com/.default']

app = msal.ConfidentialClientApplication(client_id, client_secret, authority=authority)

result = app.acquire_token_for_client(scopes)
access_token = result['access_token']
print(access_token)

在哪里可以找到许可证 SKU ID?我在 Portal 中找不到它。或者有没有办法从图形调用或powershell中获取它?

获得 SkuId 后,我想将该许可证分配给用户。这是我迄今为止设法获得的许可证分配文档,但在 SkuId 部分丢失了:

https://learn.microsoft.com/en-us/graph/api/user-assignlicense?view=graph-rest-1.0&tabs=http

python-3.x licensing microsoft-graph-sdks sku msal
1个回答
0
投票

要获取 Office 365 许可证的 SKU IDs 的值,您可以使用以下图形 API 调用:

GET https://graph.microsoft.com/v1.0/subscribedSkus?$select=skuPartNumber,skuId

我注册了一个 Azure AD 应用程序并授予了 API 权限:

enter image description here

我使用下面的Python代码来获取访问令牌并打印组织中现有许可证的SKU ID

import msal
import requests

client_id = "appID"
client_secret= "secret"
tenant_id = "tenantID"
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ['https://graph.microsoft.com/.default']

app = msal.ConfidentialClientApplication(client_id, client_secret, authority=authority)

result = app.acquire_token_for_client(scopes)
access_token = result['access_token']
print(access_token)

url = "https://graph.microsoft.com/v1.0/subscribedSkus?$select=skuPartNumber,skuId"
headers = {
    "Authorization": "Bearer " + access_token
}

response = requests.get(url, headers=headers)
data = response.json()

for sku in data['value']:
    print("\nSKU Part Number:", sku['skuPartNumber'])
    print("SKU ID:", sku['skuId'])
    print()

回复:

enter image description here

您可以通过添加几行以下Python代码向用户分配许可证:

import msal
import requests

client_id = "appID"
client_secret= "secret"
tenant_id = "tenantID"
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ['https://graph.microsoft.com/.default']

app = msal.ConfidentialClientApplication(client_id, client_secret, authority=authority)

result = app.acquire_token_for_client(scopes)
access_token = result['access_token']
print(access_token)

url = "https://graph.microsoft.com/v1.0/users/xxxxxxxxx/assignLicense"
headers = {
    "Authorization": "Bearer " + access_token,
    "Content-type": "application/json"
}

payload = {
    "addLicenses": [
        {
            "skuId": "c7df2760-2c81-4ef7-b578-5b5392b571df"
        }
    ],
    "removeLicenses": []
}

response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(response.json())

回复:

enter image description here

为了确认,我在门户中检查了相同的内容,其中

Office 365 E5
许可证已成功分配给用户:

enter image description here

参考: 列出订阅的Skus - Microsoft Graph v1.0

© www.soinside.com 2019 - 2024. All rights reserved.