Retrieving Cloud Flows using Python
Providing a step by step guide on how to retrieve Cloud Flows using Python
Leveraging Code you can retrieve a list of Power Automate Flows Created in your environment, However the thing that needs to be taken into account is that the flows created under my flows in the https://make.powerautomate.com are not retrieved as these are not registered in the Process Workflows Data-verse Table and are need to be created inside a solution for the script to list them, this could be useful for auditing purposes to know the amount of flows inside multiple solutions in an environment
Before writing the python script there are some pre-requisites that are needed to be addressed:
Create an App Registration by going to https://entra.microsoft.com then under applications go to App registrations and then Create a New Registration
When the App Registration is created get Note down the Tenant ID, Application(Client) ID and then under Manage > Certificates and Security create a new Client Secret as these will be used to generate the Oauth token
Navigate to the https://admin.powerplatform.microsoft.com and then under Environments navigate to your Developer or Live Environment against which you want to retrieve the flows for
Note down the Environment URL as this will be needed as the resource and scope when creating our script, this is present in the Environments Details Section.
In the Environment page in the Access Section then Users Select See All, you will see a list of users who have been given permissions to perform various functions against the environment, we need to provide the application similar roles as well, you should see a description saying Looking for application users? Click here to go to the app users list
Select New App user then Add the App Registration that was created and provide the relevant security roles, I provided the System Administrator App since it was a test tenant and apply the changes
Navigate to the https://make.powerapps.com and click on the settings gear and select Developer Resources and note down the Web API Endpoint
With everything gathered and confirmed the python script is provided as below
Python Script
import requests
tenantID = "Tenant ID"
clientId = "Client ID"
appSecret = "APP Secret"
sourceAppIdUri = "Environment URL"
oAuthUri = f"https://login.microsoftonline.com/{tenantID}/oauth2/token"
authBody = {
"scope": sourceAppIdUri,
"resource" : sourceAppIdUri,
"client_id" : clientId,
"client_secret" : appSecret,
"grant_type" :"client_credentials"
}
response = requests.post(url=oAuthUri,data=authBody)
token = response.json()["access_token"]
headers = {
"Content-Type" : "application/json",
"Accept" : "application/json",
"Authorization" : f"Bearer {token}",
}
Web_API_Endpoint = "https://xyz.api.crm.dynamics.com/api/data/v9.2"
Web_API_Complete = f"{Web_API_Endpoint}/workflows?$filter=category eq 5 and statecode eq 1"
Web_API_response = requests.get(url=Web_API_Endpoint,headers=headers)
print(Web_API_response.json())
Code Explanation
First we import the requests module for working with the Web API Endpoints and initiating a POST and GET request
We then store the Tenant ID, Client ID, APP Secret and the Environment URL as variables
Then we create a post request body passing in the above parameters and then creating a post request to the Oauth Token Endpoint
Once we get a successful response we create a headers dictionary and pass in the access token created from the above as well as Content-Type and Accept Paramter
We then initiate a Get request to the API Endpoint with a category equals 5 and status equals 1, Category 5 means bring me back only the cloud flows only which can be instant, Automate or Scheduled and status 1 means that are currently active
I hope you found this blog useful, and I am open to feedback :)