title: Access Power BI API with Python
body: <p>My requirement is to push real time data into Power BI using Python to first read from a database and then send the data inside a Streaming dataset in Power BI.</p>
  
  <p>The first thing I want is to make a simple "get" call to Power BI.</p>
  
  <p>The <a href="https://docs.microsoft.com/en-gb/azure/active-directory/develop/active-directory-protocols-oauth-code#oauth-20-authorization-flow" rel="nofollow noreferrer">official documentation</a> explains the processes of connecting to Power BI via the REST API for either a Client App or a Web App.
  However, I'm using Python - not sure if that is either a client app or a web app.</p>
  
  <p>Anyway, I am able to get the accessToken using the adal library and the method <a href="https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/client_credentials_sample.py" rel="nofollow noreferrer">.acquire_token_with_client_credentials</a>, which asks for authority_uri, tenant, client_id and client_secret (notice this is not asking for username and password).
  By the way, I've also tried getting the accessToken with <a href="https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/refresh_token_sample.py" rel="nofollow noreferrer">.acquire_token_with_username_password</a>, but that didn't work.</p>
  
  <p>Unfortunately, when I use the below code with the obtained accessToken, I get a response 403.</p>
  
  <pre><code>#accessToken is received using the adal libary 
  headers = {'Authorization': 'Bearer ' + accessToken, 'Content-Type': 'application/json'}
  read_datasets = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers)
  #shockingly, this will result in a response 403
  </code></pre>
  
  <p>After reading other stackoverflow posts and looking at console apps, I believe the reason this doesn't work is because there is no user sign-in process.</p>
  
  <p><a href="https://stackoverflow.com/questions/32341877/cant-get-client-credentials-access-token-to-authorize-power-bi?rq=1">This thread</a> mentions that using Client Credentials is not enough (it is enough to get the accessToken, but not enough to use the APIs)</p>
  
  <p>Not sure how to proceed, but what I need is perhaps a way to keep using this <a href="https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/client_credentials_sample.py" rel="nofollow noreferrer">adal template</a> that gives me the accessToken, and also to provide my username and password (if required), and together with the accessToken, to access the APIs.</p>