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
bohara2000
New Member

Using Power BI REST API to create a Cosmos DB datasource

I am getting a "DMTS_InvalidEncryptionAlgorithmError"  code when I try to programmatically create a CosmosDB datasource in Power BI using the REST APIs.  

 

Here's the code I try to use, minus credentials

 

AuthorizationBearer [token]
Content-typeapplication/json

{
    "datasourceName":  "MAD365-DS-SGMC",
    "credentialDetails":  {
                          "encryptedConnection":  "Encrypted",
                          "credentialType":  "Key",
                          "credentials":  "{\"credentialData\":[{\"name\":\"key\"\"value\"\"[Cosmos DB key]\"}
                        ]}",
                          "privacyLevel":  "None",
                          "encryptionAlgorithm":  "None"
    },
    "dataSourceType":  "Extension",
    "connectionDetails":  "{\"extensionDataSourceKind\"\"DocumentDB\"\"extensionDataSourcePath\":\"[Cosmos DB URI]:443\"}"
}
 
Any help would be greatly appreciated. Thanks.
1 REPLY 1
Jayendran
Solution Sage
Solution Sage

Hi @bohara2000 ,

 

You need to encrypt the credentials and call the API due to the PowerBI security.

 

Please see the below reference

https://docs.microsoft.com/en-us/power-bi/developer/encrypt-credentials

 

The below C# code helps you to do the encryption 

public static class AsymmetricKeyEncryptionHelper
{

    private const int SegmentLength = 85;
    private const int EncryptedLength = 128;

    public static string EncodeCredentials(string credentials, string publicKeyExponent, string publicKeyModulus)
    {
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(EncryptedLength * 8))
        {
            var parameters = rsa.ExportParameters(false);
            parameters.Exponent = Convert.FromBase64String(publicKeyExponent);
            parameters.Modulus = Convert.FromBase64String(publicKeyModulus);
            rsa.ImportParameters(parameters);
            return Encrypt(credentials, rsa);
        }
    }

    private static string Encrypt(string plainText, RSACryptoServiceProvider rsa)
    {
        byte[] plainTextArray = Encoding.UTF8.GetBytes(plainText);

        // Split the message into different segments, each segment's length is 85. So the result may be 85,85,85,20.
        bool hasIncompleteSegment = plainTextArray.Length % SegmentLength != 0;

        int segmentNumber = (!hasIncompleteSegment) ? (plainTextArray.Length / SegmentLength) : ((plainTextArray.Length / SegmentLength) + 1);

        byte[] encryptedData = new byte[segmentNumber * EncryptedLength];
        int encryptedDataPosition = 0;

        for (var i = 0; i < segmentNumber; i++)
        {
            int lengthToCopy;

            if (i == segmentNumber - 1 && hasIncompleteSegment)
                lengthToCopy = plainTextArray.Length % SegmentLength;
            else
                lengthToCopy = SegmentLength;

            var segment = new byte[lengthToCopy];

            Array.Copy(plainTextArray, i * SegmentLength, segment, 0, lengthToCopy);

            var segmentEncryptedResult = rsa.Encrypt(segment, true);

            Array.Copy(segmentEncryptedResult, 0, encryptedData, encryptedDataPosition, segmentEncryptedResult.Length);

            encryptedDataPosition += segmentEncryptedResult.Length;
        }

        return Convert.ToBase64String(encryptedData);
    }
}

 

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.