<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I update the Credentials after I upload a report to POWER BI in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/223597#M7064</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/34478"&gt;@Jeff_973&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;After I publish a report to the Power BI Service, I cannot embed the report until I set the credentials for the source of the data. Currently, I have to log into Power Bi portal and do this manually. I would like to programmatically set the credentials using the Microsoft.PowerBi.API.V2.Models SDK. Is there any good examples out there on how to do this? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I call the PowerBIClient.Datasets.GetGatewayDatasourcesInGroupWithHttpMessagesAsync to get a reference to the GatewayDatasource object and then call the PowerBIClient.Gateways.UpdateDatasourceWithHttpMessagesAsync to update the credentials. Is this correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;One issue I am having is constructing a CredentialDetails obj which is needed to create an UpdateDatasourceRequest object which is one of the parameters to call the UpdateDatasource method.&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/34478"&gt;@Jeff_973&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;You could check my last reply in this &lt;A href="https://community.powerbi.com/t5/Developer/Automating-Power-BI-Gateway-administration-by-using-Powershell/m-p/207528#M6567" target="_self"&gt;thread&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;public static class AsymmetricKeyEncryptionHelper
    {

        private const int SegmentLength = 85;
        private const int EncryptedLength = 128;


        /// &amp;lt;summary&amp;gt;
        /// 
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="userName"&amp;gt;&amp;lt;/param&amp;gt; the datasouce user name
        /// &amp;lt;param name="password"&amp;gt;&amp;lt;/param&amp;gt; the datasource password
        /// &amp;lt;param name="gatewaypublicKeyExponent"&amp;gt;&amp;lt;/param&amp;gt; gateway publicKey Exponent field, you can get it from the get gateways api response json
        /// &amp;lt;param name="gatewaypublicKeyModulus"&amp;gt;&amp;lt;/param&amp;gt; gateway publicKey Modulus field, you can get it from the get gateways api response json
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static string EncodeCredentials(string userName, string password, string gatewaypublicKeyExponent, string gatewaypublicKeyModulus)
        {
            // using json serializer to handle escape characters in username and password
            var plainText = string.Format("{{\"credentialData\":[{{\"value\":{0},\"name\":\"username\"}},{{\"value\":{1},\"name\":\"password\"}}]}}", JsonConvert.SerializeObject(userName), JsonConvert.SerializeObject(password));
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(EncryptedLength * 8))
            {
                var parameters = rsa.ExportParameters(false);
                parameters.Exponent = Convert.FromBase64String(gatewaypublicKeyExponent);
                parameters.Modulus = Convert.FromBase64String(gatewaypublicKeyModulus);
                rsa.ImportParameters(parameters);
                return Encrypt(plainText, 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 &amp;lt; segmentNumber; i++)
            {
                int lengthToCopy;

                if (i == segmentNumber - 1 &amp;amp;&amp;amp; 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);
        }
    }&lt;/PRE&gt;
&lt;P&gt;You can use it as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;var credentials = AsymmetricKeyEncryptionHelper.EncodeCredentials(username, password, gateway.PublicKey.Exponent, gateway.PublicKey.Modulus);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Aug 2017 07:05:30 GMT</pubDate>
    <dc:creator>Eric_Zhang</dc:creator>
    <dc:date>2017-08-02T07:05:30Z</dc:date>
    <item>
      <title>How do I update the Credentials after I upload a report to POWER BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/223243#M7051</link>
      <description>&lt;P&gt;After I publish a report to the Power BI Service, I cannot embed the report until I set the credentials for the source of the data. Currently, I have to log into Power Bi portal and do this manually. I would like to programmatically set the credentials using the Microsoft.PowerBi.API.V2.Models SDK. Is there any good examples out there on how to do this? &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I call the PowerBIClient.Datasets.GetGatewayDatasourcesInGroupWithHttpMessagesAsync to get a reference to the GatewayDatasource object and then call the PowerBIClient.Gateways.UpdateDatasourceWithHttpMessagesAsync to update the credentials. Is this correct?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One issue I am having is constructing a CredentialDetails obj which is needed to create an UpdateDatasourceRequest object which is one of the parameters to call the UpdateDatasource method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 17:59:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/223243#M7051</guid>
      <dc:creator>Jeff_973</dc:creator>
      <dc:date>2017-08-01T17:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I update the Credentials after I upload a report to POWER BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/223597#M7064</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/34478"&gt;@Jeff_973&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;After I publish a report to the Power BI Service, I cannot embed the report until I set the credentials for the source of the data. Currently, I have to log into Power Bi portal and do this manually. I would like to programmatically set the credentials using the Microsoft.PowerBi.API.V2.Models SDK. Is there any good examples out there on how to do this? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I call the PowerBIClient.Datasets.GetGatewayDatasourcesInGroupWithHttpMessagesAsync to get a reference to the GatewayDatasource object and then call the PowerBIClient.Gateways.UpdateDatasourceWithHttpMessagesAsync to update the credentials. Is this correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;One issue I am having is constructing a CredentialDetails obj which is needed to create an UpdateDatasourceRequest object which is one of the parameters to call the UpdateDatasource method.&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/34478"&gt;@Jeff_973&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;You could check my last reply in this &lt;A href="https://community.powerbi.com/t5/Developer/Automating-Power-BI-Gateway-administration-by-using-Powershell/m-p/207528#M6567" target="_self"&gt;thread&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;public static class AsymmetricKeyEncryptionHelper
    {

        private const int SegmentLength = 85;
        private const int EncryptedLength = 128;


        /// &amp;lt;summary&amp;gt;
        /// 
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="userName"&amp;gt;&amp;lt;/param&amp;gt; the datasouce user name
        /// &amp;lt;param name="password"&amp;gt;&amp;lt;/param&amp;gt; the datasource password
        /// &amp;lt;param name="gatewaypublicKeyExponent"&amp;gt;&amp;lt;/param&amp;gt; gateway publicKey Exponent field, you can get it from the get gateways api response json
        /// &amp;lt;param name="gatewaypublicKeyModulus"&amp;gt;&amp;lt;/param&amp;gt; gateway publicKey Modulus field, you can get it from the get gateways api response json
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static string EncodeCredentials(string userName, string password, string gatewaypublicKeyExponent, string gatewaypublicKeyModulus)
        {
            // using json serializer to handle escape characters in username and password
            var plainText = string.Format("{{\"credentialData\":[{{\"value\":{0},\"name\":\"username\"}},{{\"value\":{1},\"name\":\"password\"}}]}}", JsonConvert.SerializeObject(userName), JsonConvert.SerializeObject(password));
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(EncryptedLength * 8))
            {
                var parameters = rsa.ExportParameters(false);
                parameters.Exponent = Convert.FromBase64String(gatewaypublicKeyExponent);
                parameters.Modulus = Convert.FromBase64String(gatewaypublicKeyModulus);
                rsa.ImportParameters(parameters);
                return Encrypt(plainText, 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 &amp;lt; segmentNumber; i++)
            {
                int lengthToCopy;

                if (i == segmentNumber - 1 &amp;amp;&amp;amp; 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);
        }
    }&lt;/PRE&gt;
&lt;P&gt;You can use it as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;var credentials = AsymmetricKeyEncryptionHelper.EncodeCredentials(username, password, gateway.PublicKey.Exponent, gateway.PublicKey.Modulus);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 07:05:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/223597#M7064</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-08-02T07:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I update the Credentials after I upload a report to POWER BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/224165#M7082</link>
      <description>&lt;P&gt;I don't&amp;nbsp;believe I have Gateway Exponent or Modules. I am using Direct Query mode for my PBIX. After uploading the report to the Power BI Service I have to update the credentials. &amp;nbsp;Here is the code from the Provision example available on GITHUB:&amp;nbsp;&lt;/P&gt;&lt;P&gt;using (var client = await CreateClient())&lt;BR /&gt;{&lt;BR /&gt;// Get the datasources from the dataset&lt;BR /&gt;var datasources = await client.Datasets.GetGatewayDatasourcesAsync(workspaceCollectionName, workspaceId, datasetId);&lt;/P&gt;&lt;P&gt;// Reset your connection credentials&lt;BR /&gt;var delta = new GatewayDatasource&lt;BR /&gt;{&lt;BR /&gt;CredentialType = "Basic",&lt;BR /&gt;BasicCredentials = new BasicCredentials&lt;BR /&gt;{&lt;BR /&gt;Username = username,&lt;BR /&gt;Password = password&lt;BR /&gt;}&lt;BR /&gt;};&lt;/P&gt;&lt;P&gt;ExecutionReport report = null;&lt;BR /&gt;switch (datasources.Value.Count)&lt;BR /&gt;{&lt;BR /&gt;case 0: return new ExecutionReport(ExecutionLevel.Error, "No datasources exist to update");&lt;BR /&gt;case 1:&lt;BR /&gt;report = new ExecutionReport(ExecutionLevel.OK, "Connection credentials updated successfully.");&lt;BR /&gt;break;&lt;BR /&gt;default:&lt;BR /&gt;report = new ExecutionReport(ExecutionLevel.Warning, string.Format("Expected one datasource, but {0} exist, Connection credentials updated for the first", datasources.Value.Count));&lt;BR /&gt;break;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Update the datasource with the specified credentials&lt;BR /&gt;await client.Gateways.PatchDatasourceAsync(workspaceCollectionName, workspaceId, datasources.Value[0].GatewayId, datasources.Value[0].Id, delta);&lt;BR /&gt;return report;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is basically what I want to do but I don't&amp;nbsp;see the PatchDatasourceAsync method in the Microsoft.PowerBI.APi.V2.Model Library.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the test code I have written to try an accomplish the update but I keep getting an internalServerError:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--I removed the two parameters from the code above&amp;nbsp;becuase I dont have a Gateway setup since my data is in the azure cloud.&lt;/P&gt;&lt;P&gt;var credentials = AsymmetricKeyEncryptionHelper.EncodeCredentials("****", "****");&lt;/P&gt;&lt;P&gt;var result = PowerBIClient.Datasets.GetGatewayDatasourcesInGroupWithHttpMessagesAsync(groupid, datasetid).Result.Body.Value;&lt;/P&gt;&lt;P&gt;CredentialDetails cd = new CredentialDetails();&lt;BR /&gt;cd.Credentials = credentials;&lt;BR /&gt;cd.CredentialType = "Basic";&lt;BR /&gt;cd.EncryptedConnection = "Encrypted";&lt;BR /&gt;cd.EncryptionAlgorithm = "RSA-OAEP";&lt;BR /&gt;cd.PrivacyLevel = "Public";&lt;/P&gt;&lt;P&gt;UpdateDatasourceRequest udr = new UpdateDatasourceRequest(cd);&lt;/P&gt;&lt;P&gt;var update = PowerBIClient.Gateways.UpdateDatasourceWithHttpMessagesAsync(result.First().GatewayId, result.First().Id, udr).Result;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 18:27:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/224165#M7082</guid>
      <dc:creator>Jeff_973</dc:creator>
      <dc:date>2017-08-02T18:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I update the Credentials after I upload a report to POWER BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/225221#M7119</link>
      <description>&lt;P&gt;What values would I use for the&amp;nbsp;gatewaypublicKeyExponent&amp;nbsp;and&amp;nbsp;gatewaypublicKeyModulus?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 22:32:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/225221#M7119</guid>
      <dc:creator>Jeff_973</dc:creator>
      <dc:date>2017-08-03T22:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I update the Credentials after I upload a report to POWER BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/229728#M7237</link>
      <description>&lt;P&gt;I'm having the exact same problem. &amp;nbsp;I've converted pretty much the rest of the old Provision sample app to the V2 API. &amp;nbsp;But this is the last bit of code I need to fix...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var delta = new GatewayDatasource&lt;BR /&gt;{&lt;BR /&gt;CredentialType = "Basic",&lt;BR /&gt;BasicCredentials = new BasicCredentials&lt;BR /&gt;{&lt;BR /&gt;Username = username,&lt;BR /&gt;Password = password&lt;BR /&gt;}&lt;BR /&gt;};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;await client.Gateways.PatchDatasourceAsync(workspaceCollectionName, workspaceId, datasources.Value[0].GatewayId, datasources.Value[0].Id, delta);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 06:24:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/229728#M7237</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-08-11T06:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I update the Credentials after I upload a report to POWER BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/365566#M10884</link>
      <description>&lt;P&gt;I am having the same issue. Any updates on where to get the below values?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;gateway.PublicKey.Exponent, gateway.PublicKey.Modulus&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 21:18:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to-POWER/m-p/365566#M10884</guid>
      <dc:creator>Dan2</dc:creator>
      <dc:date>2018-02-26T21:18:34Z</dc:date>
    </item>
  </channel>
</rss>

