Forum Discussion
programmatic data refresh using api
- Anonymous9 years ago
Thanks for the response Eric_Zhang, it let me know that what I was trying to achieve could be done. I did some more digging and was able to modify the function in the PS script to use supplied credentials:
function GetAuthToken { $adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" $adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll" [System.Reflection.Assembly]::LoadFrom($adal) | Out-Null [System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null $redirectUri = "https://login.live.com/oauth20_desktop.srf" $resourceAppIdURI = "https://analysis.windows.net/powerbi/api" $authority = "https://login.windows.net/common/oauth2/authorize"; $userName = "[email protected]" $password = "MyPW" $creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $userName,$password $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds) return $authResult }Obviously I won't be hardcoding these credentials in the script but for testing purposes it works!
EDIT: it looks like my code block has been invaded with emoji's. If anyone is wondering you would replace the emoji with a colon ":" and the letter "P".
Thanks for the response Eric_Zhang, it let me know that what I was trying to achieve could be done. I did some more digging and was able to modify the function in the PS script to use supplied credentials:
function GetAuthToken
{
$adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
$redirectUri = "https://login.live.com/oauth20_desktop.srf"
$resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
$authority = "https://login.windows.net/common/oauth2/authorize";
$userName = "[email protected]"
$password = "MyPW"
$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $userName,$password
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds)
return $authResult
}Obviously I won't be hardcoding these credentials in the script but for testing purposes it works!
EDIT: it looks like my code block has been invaded with emoji's. If anyone is wondering you would replace the emoji with a colon ":" and the letter "P".
Hi rossnruthie,
I think I am using the same basic code as you to do refresh. I too am being prompted to login to PowerBI and a windows security login.
Where in powershell code would I put the code you have below? Or you post your entire script?
Here is my working script:
###########################################################################################
# This sample script calls the Power BI API to progammtically trigger a refresh for the dataset
# It then calls the Power BI API to progammatically to get the refresh history for that dataset
# For full documentation on the REST APIs, see:
# https://msdn.microsoft.com/en-us/library/mt203551.aspx
# Instructions:
# 1. Install PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell) and the Azure PowerShell cmdlets (https://aka.ms/webpi-azps)
# 2. Set up a dataset for refresh in the Power BI service - make sure that the dataset can be
# updated successfully
# 3. Fill in the parameters below
# 4. Run the PowerShell script
# Parameters - fill these in before running the script!
# =====================================================
# An easy way to get group and dataset ID is to go to dataset settings and click on the dataset
# that you'd like to refresh. Once you do, the URL in the address bar will show the group ID and
# dataset ID, in the format:
# app.powerbi.com/groups/{groupID}/settings/datasets/{datasetID}
$groupID = "me" # the ID of the group that hosts the dataset. Use "me" if this is your My Workspace
$datasetID = "d234581a-734c-473c-be6c-e72f54ff9058" # the ID of the dataset that hosts the dataset
# AAD Client ID
# To get this, go to the following page and follow the steps to provision an app
# https://dev.powerbi.com/apps
# To get the sample to work, ensure that you have the following fields:
# App Type: Native app
# Redirect URL: urn:ietf:wg:oauth:2.0:oob
# Level of access: all dataset APIs
$clientId = "fbeb6bda-74a4-4a51-9c92-38a22c172a49"
# End Parameters =======================================
# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
function GetAuthToken
{
$adal = "C:\Program Files\On-premises data gateway\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "C:\Program Files\On-premises data gateway\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
$authority = "https://login.microsoftonline.com/common/oauth2/authorize";
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
return $authResult
}
# Get the auth token from AAD
$token = GetAuthToken
# Building Rest API header with authorization token
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$token.CreateAuthorizationHeader()
}
# properly format groups path
$groupsPath = ""
if ($groupID -eq "me") {
$groupsPath = "myorg"
} else {
$groupsPath = "myorg/groups/$groupID"
}
# Refresh the dataset
$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method POST –Verbose
# Check the refresh history
$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method GET –Verbose
###########################################################################################
Here are my two user prompts:
- Anonymous8 years agoNot applicable
Hi mcguija
It looks like you are just using the redirect for the AcquireToken call. This is what will pop up your login screen. What you need to do is modify your AcquireToken to pass in a predefined user credential. This will make the authentication silent and prevent the login from popping up.
Here is an example:
$groupID = "12345" $datasetID = "54321" $clientId = "67890" #Username and PW of the account that will be executing the refresh $userName = "[email protected]" $password = "userPasswordValue" # End Parameters =======================================
function GetAuthToken
{
$adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
#Don't need this
#$redirectUri = "https://login.live.com/oauth20_desktop.srf"#"urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
$authority = "https://login.windows.net/common/oauth2/authorize";
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $userName,$password
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds)
return $authResult
}
# Get the auth token from AAD
$token = GetAuthToken
$tokenHere I am creating an active directory user credential and assigning the UN/PW
$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $userName,$password
Here I am taking the credentials I created in the previous step and passing it into the AcquireToken call. This is different from what you are doing.
#My AcquireToken Call passing in the user credential i created:
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds)
#Your AcquireToken Call using the redirect and forcing a sign in pop-up:
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")Hope this helps.
- Anonymous8 years agoNot applicable
This really helped me to skip SSO authentication step.
- HTAN8 years agoNew Member
I have this error message. And I have download and replace with older version of dll files but I still get same error. Can anyone help? Thanks in advance.
Method invocation failed because [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext] does not contain a method named 'AcquireToken'.
- jbruns20206 years agoNew Member
Is 'myorg' truly supposed to be part of the URL or is it standing for ones own comany id of some sort?