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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
BlocksCanFly
Regular Visitor

Error Creating PowerBI on-premise datasource using RestAPI

Dear community I recently want to create fully automated process to add and recycle access keys to PowerBI datasource and i have problem with PowerBI Rest API. I can't create or modify datasource because of problems with key value. 

I'm taking public key and modulus from datagateway 

'publicKey': {'exponent': 'AQAB',
  'modulus': '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000=='
    }

and I'm trying to encode this using python function.

from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
import base64

modulus_b64 = ""
exponent_b64 = ""
secret = ""

modulus = int.from_bytes(base64.b64decode(modulus_b64), byteorder=
'big')
exponent = int.from_bytes(base64.b64decode(exponent_b64), byteorder='big')
public_numbers = rsa.RSAPublicNumbers(exponent, modulus)
public_key = public_numbers.public_key(default_backend())
encrypted_secret = public_key.encrypt(

    secret.encode(),
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)

encrypted_secret_b64 = base64.b64encode(encrypted_secret).decode()

 

after that i'm sending put request to
https://api.powerbi.com/v1.0/myorg/gateways/{gateway}/datasources
and generate body information accordingly to Microsoft documentation 
{

    "connectionDetails": "{\"extensionDataSourceKind\": \"Databricks\", \"extensionDataSourcePath\": \"{\\\"host\\\": \\\"adb-0000000000000000000.0.azuredatabricks.net\\\", \\\"httpPath\\\": \\\"/sql/1.0/warehouses/0000000000000\\\"}\"}",
    "credentialDetails": {
        "credentialType": "Key",
        "credentials": "{\"credentialData\": [{\"name\": \"key\", \"value\": \"daasd==\"}]}",
        "encryptedConnection": "Encrypted",
        "encryptionAlgorithm": "RSA-OAEP",
        "privacyLevel": "Organizational"
    },
    "dataSourceName": "DB01",
    "dataSourceType": "Extension"
}

i'm allways getting this response. Do you know what might be couse of that? 

{'error': {'code': 'DM_GWPipeline_UnknownError',  'pbi.error': {'code': 'DM_GWPipeline_UnknownError',  'parameters': {},  'details': [{'code': 'DM_ErrorDetailNameCode_UnderlyingErrorMessage',  'detail': {'type': 1,  'value': 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. '}},  {'code': 'DM_ErrorDetailNameCode_UnderlyingHResult',  'detail': {'type': 1, 'value': '-2146233033'}}]}}}
2 REPLIES 2
BlocksCanFly
Regular Visitor

Hi i though the same and i used microsoft tamplate from github. PowerBI-Developer-Samples/Python/Encrypt credentials at master · microsoft/PowerBI-Developer-Samples...
and still didn't worked. So i tired to make same request via gui and scout changes and find out this request use different 17njPpZo9l.png

lbendlin
Super User
Super User

you seem to be decoding one too many times

 

encrypted_secret_b64 = base64.b64encode(encrypted_secret).decode()

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.