Forum Discussion

pedromasou's avatar
pedromasou
New Member
7 years ago
Solved

Azure Powershell automatic log-in to refresh Power BI

I'm currently developing a script to automatically refresh one of my Power BI datasets using Power BI's REST API. This script is a .ps1 file, called from my Python code as you can see below. I follow...
  • v-jiascu-msft's avatar
    7 years ago

    Hi pedromasou,

     

    I would suggest you use pure Python and ADAL instead. Please refer to azure-activedirectory-library-for-python and active-directory/develop/v1-oauth2-implicit-grant-flow

    If your Azure account applies compulsory two-factor authentication, I'm afraid user action is necessary. If not, the implicit grant flow could be the solution. You can get the access token with a username and a password. 

    Please also refer to this Python script.

     

    # adal is the azure-activedirectory-library-for-python. install it first.
    from adal import AuthenticationContext
    import requests
    import json
    
    
    # get access token implictly with username and password.
    def get_token():
        client_id = "a30***1ae1dcde27e"
        resource_uri = "https://analysis.windows.net/powerbi/api"
        user_name = "d**.com"
        user_password = "password"
    
        auth_context = AuthenticationContext("https://login.microsoftonline.com/your_tenant_name")
        token_response = auth_context.acquire_token_with_username_password(resource_uri, user_name, user_password, client_id)
        token = "Bearer " + token_response["accessToken"]
        return token
    
    
    def refresh():
        token = get_token()
        headers = {"Authorization": token, "Content-type": "application/json"}
        post_url = "https://api.powerbi.com/v1.0/myorg/groups/d002f024-*******bc75fb104/datasets/bbd609a0-*******c-c949d8a8ff9d/refreshes"
        body = {
          "notifyOption": "MailOnCompletion"
        }
    
        r = requests.post(post_url, headers = headers, data = json.dumps(body))
        return r.status_code
    
    	
    if __name__ == "__main__":
    	refresh()
    

     

    Best Regards,
    Dale