cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
alexandruMP
Regular Visitor

OAuth2 Dataset Credentails Update after take over fails

I am trying to take over a dataset that has data in ADX and although the take over is successful, the dataset credentials need to be updated.

*SPN and user have full admin rights on workspace and Adx cluster

I have the following script:

 

 

 

 

 

$PWord = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $clientId, $PWord


Connect-PowerBIServiceAccount -Tenant $Tenant -ServicePrincipal -Credential $Credential



################ Taking over dataset as SPN  ####################

Invoke-PowerBIRestMethod -Url "groups/$($Workspace)/datasets/$($Dataset)/Default.TakeOver" -Method POST -Body "" 

####################  "Resetting" Credentials towards datasource of dataset ####################


# Apparently we need the id of the "Gateway" that the datasets resides within. If it doesn't have a gateway (if it's a cloud data source for instance) it uses Microsofts "Default" gateway. We need its ID.
$BounGateway=Invoke-PowerBIRestMethod -Url "groups/$($Workspace)/datasets/$($Dataset)/Default.GetBoundGatewayDataSources" -Method GET | ConvertFrom-Json 

$Accesstoken = Get-PowerBiAccessToken

$Body =
@{
credentialDetails = @{
credentialType = "OAuth2";
credentials = "{`"credentialData`":[{`"name`":`"accessToken`", `"value`":`"$($Accesstoken.Authorization)`"}]}";
encryptedConnection = "Encrypted";
encryptionAlgorithm = "None";
privacyLevel = "None"
}
} | ConvertTo-Json

Write-Output $Body

Invoke-PowerBIRestMethod -Url "gateways/$($BounGateway.value.gatewayId)/datasources/$($BounGateway.value.id)" -Method PATCH -Body $Body | ConvertFrom-Json 

 

 

 

 

 

 
The body of the request I have created it using the example from here: https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/update-datasource#oauth2-credentials-exa...

Finally, as you can see in the script I use an SPN and an access token for it and the last PATCH request returns: 

 

 

 

 

 

Invoke-PowerBIRestMethod:
Line |
  44 |  Invoke-PowerBIRestMethod -Url "gateways/$($BounGateway.value.gatewayI …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | One or more errors occurred. (Response status code does not indicate success: 400 (Bad Request).)

 

 

 

 

 


I tried to use an actual user to do this operation, but the PATCH request with the user access token returns 401:

 

 

 

 

 

Invoke-PowerBIRestMethod:
Line |
  14 |  Invoke-PowerBIRestMethod -Url "gateways/$($BounGateway.value.gatewayI …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | One or more errors occurred. (Response status code does not indicate success: 401 (Unauthorized).)

 

 

 

 

 

 

This story seems to be related to this ,  but it is unanswered for OAuth2.

I was wondering is somebody has figured what the issue is, as far as I have searched the only working credential types are "key" and "basic".

 

@NandanHegde @Jayendran 

1 REPLY 1
V-lianl-msft
Community Support
Community Support

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.