0

This appears to be a nightmare, sure its easy to upgrade the nuget package to 3.11 I think the latest is, but then nothing at all compiles. So you fix the compile errors, and then it doesn't work. I'm getting an error when it tries to create the PowerBI client.

Getting the token and also creating the client appears to be totally different to v2.

This is my code:

   public PowerBiConfig GetPowerBiConfig(string reportId)
    {

        var result = new PowerBiConfig();
        try
        {
            if (!Guid.TryParse(reportId, out var _))
            {
                result.ErrorMessage = $"Invalid report guid: {reportId}";
                return result;
            }

            var credential = new UserPasswordCredential(_powerBiProMasterUsername, _powerBiProMasterPassword);

            var authenticationContext = new AuthenticationContext(AuthorityUrl);

            // Taken from https://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously
            var authenticationResult = authenticationContext.AcquireTokenAsync(ResourceUrl, dataArchiverSettings.PowerBiApplicationId, credential).GetAwaiter().GetResult();
            if (authenticationResult == null)
            {
                result.ErrorMessage = "Authentication Failed.";
                return result;
            }

            var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

            using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
            {
                var report = client.Reports.GetReportInGroup(dataArchiverSettings.PowerBiWorkspaceId, reportId);
                if (report == null)
                {
                    result.ErrorMessage = $"No report with the ID {reportId} was found in the workspace.";
                    return result;
                }

                var datasets = client.Datasets.GetDatasetById(dataArchiverSettings.PowerBiWorkspaceId, report.DatasetId);
                result.IsEffectiveIdentityRequired = datasets.IsEffectiveIdentityRequired;
                result.IsEffectiveIdentityRolesRequired = datasets.IsEffectiveIdentityRolesRequired;

                GenerateTokenRequest tokenRequest;

                if (datasets.IsEffectiveIdentityRequired == true)
                {
                    var username = UserHelper.GetCurrentUser();
                    var roles = _userService.GetRolesForUser(username);
                    tokenRequest = new GenerateTokenRequest(accessLevel: "view",
                        identities: new List<EffectiveIdentity>
                        {
                            new EffectiveIdentity(username: username,
                                roles: new List<string> (roles.Select(x=> x.RoleName)),
                                datasets: new List<string> {datasets.Id})
                        });
                }
                else
                {
                    tokenRequest = new GenerateTokenRequest(accessLevel: "view");
                }

                var tokenResponse =
                    client.Reports.GenerateTokenInGroup(dataArchiverSettings.PowerBiWorkspaceId, report.Id,
                        tokenRequest);

                if (tokenResponse == null)
                {
                    result.ErrorMessage = "Failed to generate embed token.";
                    return result;
                }

                // Generate Embed Configuration.
                result.EmbedToken = tokenResponse;
                result.EmbedUrl = report.EmbedUrl;
                result.Id = report.Id.ToString();

                result.WorkloadResourceName = dataArchiverSettings.PowerBiWorkloadResourceName.Trim();
            }

        }
        catch (HttpOperationException exc)
        {
            result.ErrorMessage =
                $"Status: {exc.Response.StatusCode} ({(int)exc.Response.StatusCode})\r\n" +
                $"Response: {exc.Response.Content}\r\n" +
                $"RequestId: {exc.Response.Headers["RequestId"].FirstOrDefault()}";
        }
        catch (Exception exc)
        {
            result.ErrorMessage = exc.ToString();
        }
        return result;
    }
Philip Johnson
  • 1,091
  • 10
  • 24

1 Answers1

2

The closest to "upgrade guide" is the announcement in Power BI blog. It looks like your code is using v2 (e.g. reportId is string, while in v3 it should be Guid).

Here is a brief summary of the changes:

What you should know about v3

Here are the key changes with this version update:

Namespaces renaming:

  • Microsoft.PowerBI.Api.V2 was changed to Microsoft.PowerBI.Api

  • Microsoft.PowerBI.Api.Extensions.V2 was changed to Microsoft.PowerBI.Api.Extensions

  • Microsoft.PowerBI.Api.V1 namespace was removed.

  • SetAllConnections and SetAllConnectionsInGroup operations are deprecated and marked as obsolete. You should use UpdateDatasources or UpdateParameters APIs instead.

  • PowerBI artifacts IDs typing was changed* from string to Guid, we recommend to work with Guid when possible.

    *Dataset ID is an exception and it’s typing will remain string.

  • ODataResponse[List[Object]] types was changed to Objects, thus returning an objects collection on responses. For example, a response of ODataResponse[List[Report]] type will now return Reports collection as the return type.

  • New credentials classes allow easier build of credentialDetails. The new classes include: BasicCredentials, WindowsCredentials, OAuth2Credentials, and more. Read Configure credentials article to learn more.

  • New encryption helper classes for easier encryption when creating CredentialDetails. For example, using AsymmetricKeyEncryptor class with a gateway public key:

GatewayPublicKey publicKey = new GatewayPublicKey
{
    Exponent = "...",
    Modulus = "..."
};
CredentialsBase credentials = new BasicCredentials("<USER>", "<PASSWORD>");
var credentialsEncryptor = new AsymmetricKeyEncryptor(publicKey);
var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.None, EncryptedConnection.Encrypted, credentialsEncryptor);

Read Configure credentials article to learn more.

  • Consistency on field names.

For example, reportKey, datasetKey, dashboardKey and tileKey was changed to reportId, datasetId, dashboardId and tileId.

  • Consistency on operations names.

For example, use GetDataset instead of GetDatasetById. The effected opertation names are imports, datasets, gateways and datasources.

  • Use enum class instead of string for enumerated types.

For example, In the generateTokenRequest, we recommend to use TokenAccessLevel.View, and not explicitly use “view” as value.

  • Required fields was marked – some fields was changed to required fields are not nullable anymore.

Examples

Change in Get Reports call if WorkspaceId is a string:

var reports = await client.Reports.GetReportsInGroupAsync(WorkspaceId);

var reports = await client.Reports.GetReportsInGroupAsync(new Guid( WorkspaceId ) );

Change in response handling if a string is expected:

report = reports.Value.FirstOrDefault(r => r.Id.Equals(ReportId, StringComparison.InvariantCultureIgnoreCase));

report = reports.Value.FirstOrDefault(r => r.Id .ToString() .Equals(ReportId, StringComparison.InvariantCultureIgnoreCase));

Change in Generate token:

var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters);

var tokenResponse = await client.Reports.GenerateTokenInGroupAsync( new Guid( WorkspaceId ), report.Id, generateTokenRequestParameters);

Change in Generate token response handling if a string is expected:

m_embedConfig.Id = report.Id;

m_embedConfig.Id = report.Id .ToString() ;

Required fields are not nullable, i.e. Expiration is not nullable and the Value property should be removed:

var minutesToExpiration = EmbedToken.Expiration .Value – DateTime.UtcNow;

var minutesToExpiration = EmbedToken.Expiration – DateTime.UtcNow;

Consistency on operations names, i.e. use GetDataset instead of GetDatasetById:

var datasets = await client.Datasets.GetDataset ById InGroupAsync(WorkspaceId, report.DatasetId);

var datasets = await client.Datasets.GetDatasetInGroupAsync(new Guid(WorkspaceId), report.DatasetId);

Andrey Nikolov
  • 12,967
  • 3
  • 20
  • 32