cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper V
Helper V

Uploading PBIX File in C# sample (HELP!)

Good morning everyone,

For centuries I'm looking to upload pbix files automatically in my workspace locate into the PowerBI Service. I

The best example is this one: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/Publishing-PBIX-Files-with-the-Power-BI-...


(This video demonstrates developing a C# console to automate uploading and publishing PBIX project files created with Power BI Desktop to PowerBI.com.)


I´ve found this article that explains how the C# works BUT when i select "Native App" it doesn't appear the "Redirect URL". The problem is that when I run the C# sample application , it requires the redirect URL.

Screen Shot 08-14-19 at 11.05 AM.PNG

There´s anyone here that knows about C# and can help me?

1 ACCEPTED SOLUTION

Hi @brunozanoelo 

 

You don't need dedicated capacity now. It has no connection with the autentication.

 

Are you running behind the code behind any proxy ? Which blocks your autentication ?

 

Can you try executing in Post man ?

 

Capture.JPG

View solution in original post

15 REPLIES 15
Solution Sage
Solution Sage

Hi @brunozanoelo 

You don't really need redirect url for a native app. All you need to use the usercredential approach which is commented on the code you need to uncomment that. Because powerbi no longer support the client id with out Azure AD registration.

 

 

 

 

 // create new ADAL authentication context 
      var authenticationContext =
        new AuthenticationContext(ProgramConstants.AzureAuthorizationEndpoint);

            //use authentication context to trigger user login and then acquire access token
            //var userAuthnResult =
            //  authenticationContext.AcquireTokenAsync(ProgramConstants.PowerBiServiceResourceUri,
            //                                          ProgramConstants.ClientID,
            //                                          new Uri(ProgramConstants.RedirectUri),
            //                                          new PlatformParameters(PromptBehavior.Auto)).Result;


            // use authentication context to trigger user sign-in and return access token 
            var userCreds = new UserPasswordCredential("UserName@MyTenent.onMicrosoft.com", "myEasyToCrackPassword");

            var userAuthnResult = authenticationContext.AcquireTokenAsync(ProgramConstants.PowerBiServiceResourceUri,
                                                                          ProgramConstants.ClientID,
                                                                          userCreds).Result;

 

 

Update your username and password in the userpasswordcredentials

 

Capture.JPG

 

Thank you so much for your reply.

I´ve changed my code as u said but now is giving me this error:

System.AggregateException
HResult=0x80131500
Message=Um ou mais erros.
Source=mscorlib
StackTrace:
em System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
em System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
em System.Threading.Tasks.Task`1.get_Result()
em PbixInstallerForPowerBI.Program.AcquireAccessToken() em D:\Bitbucket\PowerBI\Integração REST\PbixInstallerForPowerBI-master\PbixInstallerForPowerBI\Program.cs:linha 54
em PbixInstallerForPowerBI.Program.Main() em D:\Bitbucket\PowerBI\Integração REST\PbixInstallerForPowerBI-master\PbixInstallerForPowerBI\Program.cs:linha 303

Exceção interna 1:
AdalServiceException: AADSTS65001: The user or administrator has not consented to use the application with ID '75d071b1-378d-4f76-bb68-da4e033cf646' named 'instalador'. Send an interactive authorization request for this user and resource.
Trace ID: 20ec587e-39c6-4afa-acbb-67dfe8c9c800
Correlation ID: a7f7e02e-ffde-453b-b331-89c2e89a3601
Timestamp: 2019-08-14 17:32:02Z

Exceção interna 2:
AdalServiceException: Response status code does not indicate success: 400 (BadRequest).

What should be?

BEST regards

Hi @brunozanoelo 

 

The admin needs to consent your application. You can do this from you Azure Active Directory for your application called instalador

 

Capture.JPG

 

Another little change in the code to force TLS1.2, pls update your code like below 

 

 System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;
            var userCreds = new UserPasswordCredential("UserName@MyTenent.onMicrosoft.com", "myEasyToCrackPassword");

            var userAuthnResult = authenticationContext.AcquireTokenAsync(ProgramConstants.PowerBiServiceResourceUri,
                                                                          ProgramConstants.ClientID,
                                                                          userCreds).Result;

@Jayendran  Thank you for the help. I really appreciate it.

 

It worked to authenticate but now I´m facing one problem in my workspace. I already set all the permissions to this app, including read/write workspaces.


Error (cannot find workspace)Error (cannot find workspace)

 

WorkspaceWorkspace

PermissonsPermissons
Any tip? I´m trying everything in Azure and nothing solve this problem.?

Warm Regards.

 

Hi @brunozanoelo ,

 

I tried the same thing it works for me.

 

Here some few tips 

 

  1. Check whether you user credentials (user name/password) having access to the workspace.
  2. Using the fiddler find what API is exactly calling and whether you are getting any error in the fiddler

I see that this is calling 

https://api.powerbi.com/v1.0/myorg/groups

https://docs.microsoft.com/en-us/rest/api/power-bi/groups/getgroups

 

It requires the scope of  Workspace.Read.All or Workspace.ReadWrite.All but from the screenshot you also having that

Capture.JPG

Hello my friend,

I´ve found that the problem is actually not in the workspace, but still in the authentication function.

See the image bellow. I think my code is correct, isn't? The client ID it´s the correct one, I already checked! 

Auth not passedAuth not passed

 

Hi @brunozanoelo 

 

Yes pls use the fiddler to find what was the failure from the API. Since it's not reproduce from my side. Fiddler will get the exact issue here

Success using the fiddlerSuccess using the fiddler

 

More permissionsMore permissions

But in the app, the errorstill persist. 

Hi @brunozanoelo 

 

Previously you said that autentication is not passed which means it should have a 200 status code. In such scenario you should see the error in fiddler.

 

However you said that fiddler is successful. It doesn't make sense !

 

However, Can you change your code to like the below

 

HttpClient client = new HttpClient();
      client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);
      client.DefaultRequestHeaders.Add("Accept", "application/json");

      HttpResponseMessage response = client.GetAsync(restUri).Result;
     var content = response.Content.ReadAsStringAsync().Result;

     Console.WriteLine(content);

this way you can get what is the result of the API.

I believe I'm getting an sucessfully authentication, since I get the IDToken from the 

Console.WriteLine(userAuthnResult.IdToken);

The consolewriteline result an empty string, BUT, take a look of this message when I debug... When the application uses the Workspace function, I get the status code 403 - Forbidden.


Status code 403??Status code 403??

empty stringempty string403403


I´m logging in with my PowerBI PRO account, I don´t have any embedded capacity in Azure portal, I need to create it and change my workspace to "dedicated capacity"? https://community.powerbi.com/t5/Service/PowerBI-Embedded-Service-403/td-p/71382


Hi @brunozanoelo 

 

You don't need dedicated capacity now. It has no connection with the autentication.

 

Are you running behind the code behind any proxy ? Which blocks your autentication ?

 

Can you try executing in Post man ?

 

Capture.JPG

View solution in original post

Got it! I´ve found the problem!

I will post this source in github in a few days. I just need to fix a few details. (I think this will help a lot of ppl too) 🙂

I´m facing one problem that when I send my dataset, he doesn't replace, even if is the same pbix name, there´s any possibility to replace the dataset? I need this because I have tons of configurations to auto refresh data.

Thank you a lot my friend.

Warm Regards,

Anonymous
Not applicable

Looking forward to see how you solved this, please post the solution on github and share here. Thank you ! 

Hi @brunozanoelo 

 

Glad you solved your problem.

 

Yes there is way to replace the dataset, using the ImportConflictHandlerMode by assigning CreateOrOverwrite

 

https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName={datasetDisplayName}&nameConflict={nameConflict}

Reference:

https://docs.microsoft.com/en-us/rest/api/power-bi/imports/postimport#importconflicthandlermode

@Jayendran , there´s no property in the dataset object to use the dataset/refresh history;

https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/

I´m trying to implement this, will keep in touch...

Helpful resources

Announcements
secondImage

Congratulations!

We are excited to announce the Power BI Super Users!

Microsoft Ignite

Microsoft Ignite with Arun Ulag

Featured Session: Drive Data Culture with Power BI- Vision, Strategy & Roadmap. Register here https://myignite.microsoft.com #MSIgnite #PowerPlatform #Power BI ​

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.

Top Kudoed Authors