如何使用 MS Graph API 和 Python 将客户端密码添加到 AAD 应用程序

How to use MS Graph API and Python to add a client secret to AAD application

我正在寻找 Python 使用 MS Graph API 的示例,为 Microsoft Azure AD 应用程序注册生成新的客户端密码。有人可以帮我吗?

这是一个示例,以及使用客户端和密钥获取令牌

请参阅 addPassword API

的文档

获取承载(和访问)令牌

# The App Registration's application (client) ID
client_id = ""

# Client secret created under App Registration blade
client_secret = ""

# Your Azure AD tenant ID
tenant_id = ""

app = msal.ConfidentialClientApplication(
    client_id         = client_id,
    client_credential = client_secret,
    authority         = f"https://login.microsoftonline.com/{tenant_id}")

scopes = ["https://graph.microsoft.com/.default"]

# Obtain bearer token from MS Graph
token = None
token = app.acquire_token_for_client(scopes = scopes)

使用 Graph 添加新的客户端密码 API

# The App Registration's object ID
app_object_id = ""    

req_uri = f"https://graph.microsoft.com/v1.0/applications/{app_object_id}/addPassword"

req_headers = {
    "Authorization": "Bearer " + token['access_token'],
    "Content-Type": "application/json"
}

req_body = json.dumps(
    {
        "passwordCredential": {
            "displayName": "Secret Description"
        }
    }
)

result = requests.post(url = req_uri, headers = req_headers, data = req_body)