Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Preparing for a certification exam? Ask exam experts all your questions on May 15th. Register now.

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
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.