I have 2 app registration (2 service principals). First of them I use as my credentials to have token. I need from my Python script to create and delete the secrets of the second service principal. Unfortunately, I did not find such an example in the documentation. How can I do that?
Advertisement
Answer
You can use the below code for your requirement :
Add Client_Secret:
JavaScript
x
21
21
1
from azure.identity import ClientSecretCredential
2
from msgraph.core import GraphClient
3
import json
4
clientid= "Serviceprincipal1"
5
clientsecret = "secret"
6
tenantid = "tenantId"
7
credentials=ClientSecretCredential(tenant_id=tenantid,client_id=clientid,client_secret=clientsecret)
8
graph_client = GraphClient(credential=credentials)
9
10
#get details of another service principal by providing the object id of the application
11
app = graph_client.get('/applications/serviceprincipal2objectid')
12
print(app.json())
13
#add new client sceret to that ad app
14
body={
15
"passwordCredential": {
16
"displayName": "NewPaasswordCreatedfromPythonSDK"
17
}
18
}
19
addpass=graph_client.post('/applications/serviceprincipal2objectid/addPassword',json=json.dumps(body))
20
print("HTTP_request_Response:",addpass.status_code)
21
Outputs:
Remove Client_Secret:
JavaScript
1
7
1
#remove a client secret for that ad app
2
body= {
3
"keyId": "1636f0ce-1b8c-46a0-a580-d0df086b91c7"## keyid of the key added earlier
4
}
5
removepass=graph_client.post('/applications/serviceprincipal2objectid/removePassword',json=body)
6
print("HTTP_request_Response:",removepass.status_code)
7
Output:
Note: MSGRAPH-core python sdk
is in Preview only and to use you have to install using pip install msgraph-core