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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
SashaSydorenko
New Member

Changing gateway datasource credentials through API

Hi Folks,

I'm failing on pretty simple task - update datasource credentials with OAuth2 token passed in payload.

Firstly, i'm generating new token to be passed with below request:

 

 

$getDatasourceTokenRequestParams = @{
        Method = 'POST'
        Uri    = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
        Body   = @{
            grant_type = 'client_credentials'
            client_id = $ClientId
            client_secret = $ServicePrincipalSecret
            scope = $ClientId+'/.default'
            audience = 'https://s*****-test.database.windows.net/'
        }
    }
    $TokenRequest = Invoke-RestMethod @getDatasourceTokenRequestParams

 

 

Then i paste this token into my request using Try It facility on

https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/update-datasource#code-try-0

 

I insert datasourceid and gatewayid and request body with the token:

 

 

{
  "credentialDetails": {
    "credentialType": "OAuth2",
    "credentials": "{\"credentialData\":[{\"name\":\"accessToken\", \"value\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik1yNS1BVWliZkJpaTdOZDFqQmViYXhib1hXMCJ9.eyJhdWQiOiI1ZjkwMGRhOC03NTUzLTQyYWItOTljYy1hM2QzNjg3ZTM3MzEiLCJpc3M
iOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vM2UyMGVjYjItOWNiMC00ZGYxLWFkN2ItOTE0ZTMxZGNkZGE0L3YyLjAiLCJpYXQiOjE2NDAyNjYyNDMsIm5iZiI6MTY0MDI2NjI0Myw
iZXhwIjoxNjQwMjcwMTQzLCJhaW8iOiJFMlpnWU5DLzhEak45UFBSOVNIOHhoYzRLODY5QWdBPSIsImF6cCI6IjVmOTAwZGE4LTc1NTMtNDJhYi05OWNjLWEzZDM2ODdlMzczMSIsImF6cGFjciI6IjE
iLCJvaWQiOiI3NjMxNmI5Zi0xYzA2LTQzZTItYTNkZi02NzQzMjk5OTUzZmQiLCJyaCI6IjAuQVJzQXN1d2dQckNjOFUydGU1Rk9NZHpkcEtnTmtGOVRkYXRDbWN5ajAyaC1OekViQUFBLiIsInN1YiI
6Ijc2MzE2YjlmLTFjMDYtNDNlMi1hM2RmLTY3NDMyOTk5NTNmZCIsInRpZCI6IjNlMjBlY2IyLTljYjAtNGRmMS1hZDdiLTkxNGUzMWRjZGRhNCIsInV0aSI6IAOI_EnwgM_-1TOcGHiXXPkQqLH5D9STh_KhgC0xgLBZV8W3eO5jMffUxRsuUIGeJigcGbeKlGLCj08k_OgbPeoyAPloGI_Ahl9x3FSE1G0kQH7ehP7wdlI
ppwL09Yi1ScId09NzZnJHtcvUOrOrqgE8oSSx3cn_BwvQnGB8UDTSEufig\"}]}",
    "encryptedConnection": "Encrypted",
    "encryptionAlgorithm": "None",
    "privacyLevel": "None"
  }
}

 

 

 

All looks great but i receiving 401 response code 😞

 

 

{
  "error": {
    "code": "PowerBINotAuthorizedException",
    "pbi.error": {
      "code": "PowerBINotAuthorizedException",
      "parameters": {},
      "details": [],
      "exceptionCulprit": 1
    }
  }
}

 

 

I assume that token i generated is incorrect, but unfortunately i didn't found any documentation on how exactly it should be generated.

Could you please provide me with the steps how to get correct token for this request?

7 REPLIES 7
V-lianl-msft
Community Support
Community Support

Hi @SashaSydorenko ,

 

Are you using service principal? If so, did you go through all the proper configuration to enable service principal access to Power BI API in your tenant and add servide principal as gatewway admin?

https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal 

https://community.powerbi.com/t5/Developer/Power-Bi-REST-API-401-Authorization-error-when-using-Serv... 


Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Liang,

Service Principal have ReadWrite scope API permissions. Not sure about gateway admin? It's some kind of role in Power BI tenant? I'm actually not using gateways because datasource is in Azure.

lbendlin
Super User
Super User

Did you grant that app/service principal the required gateway admin permissions and the ReadWrite scope on the tenant?

Hi lbendlin,

Service Principal have ReadWrite scope API permissions. Not sure about gateway admin? It's some kind of role in Power BI tenant? I'm actually not using gateways because datasource is in Azure.

Your original question stated "gateway datasource credentials".  Please clarify.

Hmmm good point. 🙂
It a bit confusing because it is how docs article is called https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/update-datasource

But yeah, my report don't need a gateway - datasourcse is Azure SQL DB.

Does i'm use wrong API to update datasource credentials in this case?

Didn't found any other way to do that.

Hi @SashaSydorenko ,

 

Did you manage to resolve this issue?

I am getting the same error.

 

Please share if you have a solution.

 

Thanks

 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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