One of my more popular posts from last year was ‘Power BI REST API”. This was shown using C# but as this topic has also been asked many times on various other forums but for Python, I thought I’d write a quick piece on that.
As with C# approach, the starting point is registering an app with Azure AD. This link will show you step by step on how to do that. Once that’s done and you have your client_id (needed for the code), the other steps like getting an authentication access token and submitting other REST requests are exactly the same as C#. The code to do just this is as follows.
The first library we’ll look at is ‘adal‘ (presumably by Microsoft-it is specific to Azure). We won’t go into details of the library, just enough to get the tokens as that’s where most people struggle with but as with Python in general, the code is short, crisp and there’s much less of it than your typical OO languages like C# to achieve the same task.
You’ll note I also brought in the json and pandas library. These were used to load the response into a dataframe for my downstream process.
An alternative to adal as well as my preferred approach is to use a generic web library such as urllib. As seen below, the code is very similar only this follows the generic REST pattern.
As before, I’ve added pandas so I can create a dataframe for one of my downstream processes. The screenshot bellows shows the dataframe output.
And finally to showcase where this may be used-SQL Server 2017 with Python integration as seen below.
So there you have it, simple demo on how to use the Python libraries for Power BI REST API. Happy coding.