Forum Discussion

lorcan17's avatar
lorcan17
Frequent Visitor
3 years ago

Unable to update Data Source credentials for SharePoint Site using REST API and Service Account

I have a Power BI dataset which is connected to a sharepoint site. I want to be able to do the following with a AAD Service Account:

  1. Takeover Datset
  2. Get Datasources for dataset
  3. Update credentials for datasource
  4. Refresh dataset

I able to perform 1 and 2 using rest api but receive an error when attempting to update credentials for a SharePoint site datasource.

 

Below is a snippet of the Python code I am using, using this guide as a reference https://learn.microsoft.com/en-us/rest/api/power-bi/gateways/update-datasource

 

 

 

 

...
access_token = asdhaudhuaihfa...
credentials = {"credentialData": {"name": "accessToken", "value": access_token}}
credentialType = "OAuth2"

def update_credentials(gateway_id, datasource_id, credentialType,
                       credentials, privacyLevel=None, useCallerAADIdentity = None,
                       useEndUserOAuth2Credentials = None):
    # PATCH https://api.powerbi.com/v1.0/myorg/gateways/{gatewayId}/datasources/{datasourceId}
    body = {
        "credentialDetails": {
            "credentialType": credentialType,
            "credentials": json.dumps(credentials),
            "encryptedConnection": "Encrypted",
            "encryptionAlgorithm": "None",
            "privacyLevel": privacyLevel,
            'useCallerAADIdentity': useCallerAADIdentity, 
            'useEndUserOAuth2Credentials': useEndUserOAuth2Credentials 
        }
    }
    url = 
    f'https://api.powerbi.com/v1.0/myorg/gateways/{gateway_id}/datasources/{datasource_id}'
    print(f"Rest API: {url}")
    api_call = requests.patch(url, data = json.dumps(body), 
    headers= {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"})
    return api_call

 

 

 


I have tried using the PBI access token, a SharePoint Site access token, trying different (True/False/Blank) combinations of useCallerAADIdentity & useEndUserOAuth2Credentials. All have returned error codes. 

Error codes:

DM_GWPipeline_Gateway_DataSourceAccessError

Specifying UseCallerOAuthIdentity requires credential type to be OAuth2

 

Any help would be greatly appreciated.

 

My App API permissions:

 

 

 

Lorcan

 

1 Reply