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

2j4z5cfb  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(167)

通过在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?我在门户网站上找不到它。或者有没有一种方法可以从graph call或powershell中获取它?
有了SkuId后,我想将该许可证分配给用户。这是许可证转让文件,我已经设法获得到目前为止,但在SkuId部分丢失:
https://learn.microsoft.com/en-us/graph/api/user-assignlicense?view=graph-rest-1.0&tabs=http

cxfofazt

cxfofazt1#

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

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

字符串
我注册了一个Azure AD应用,并授予了API权限


的数据
我使用下面的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()

回复:



您可以通过添加几行代码,使用下面的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())

回复:



为了确认这一点,我在门户中进行了同样的检查,其中Office 365 E5许可证已成功分配给用户:


参考:List subscribedSkus - Microsoft Graph v1.0

相关问题