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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Zirochka
Frequent Visitor

get an error in example "Push data into a Power BI dataset"

Hi,

I'm trying to figure out how push data into datasets through PowerBI API. Because of it I tried to execute example Push data into a Power BI dataset and at Step 5: Add rows to a Power BI table I got an error on this line:

var response = (HttpWebResponse)request.GetResponse();

Text of error:

Unhandled Exception: System.Net.WebException: The remote server returned an error: (404) Not Found.
at System.Net.HttpWebRequest.GetResponse()
at PushDataApp.Program.AddRows(String datasetId, String tableName) in c:\users\oleg\documents\visual studio 2017\Projects\PushDataApp\PushDataApp\Program.cs:line 174
at PushDataApp.Program.Main(String[] args) in c:\users\oleg\documents\visual studio 2017\Projects\PushDataApp\PushDataApp\Program.cs:line 31

Why is this happening?

 

And one more thing. In this example at Step 2: Get an authentication access token on next line:

string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

opens window where you need to enter email and password. Is there any way not to show this window and enter email/password in or through the program? I mean I (and users of my future program too) don't want to enter this data manually.

 

I'll appreciate any help.

2 REPLIES 2
Eric_Zhang
Microsoft Employee
Microsoft Employee


@Zirochka wrote:

Hi,

I'm trying to figure out how push data into datasets through PowerBI API. Because of it I tried to execute example Push data into a Power BI dataset and at Step 5: Add rows to a Power BI table I got an error on this line:

var response = (HttpWebResponse)request.GetResponse();

Text of error:

Unhandled Exception: System.Net.WebException: The remote server returned an error: (404) Not Found.
at System.Net.HttpWebRequest.GetResponse()
at PushDataApp.Program.AddRows(String datasetId, String tableName) in c:\users\oleg\documents\visual studio 2017\Projects\PushDataApp\PushDataApp\Program.cs:line 174
at PushDataApp.Program.Main(String[] args) in c:\users\oleg\documents\visual studio 2017\Projects\PushDataApp\PushDataApp\Program.cs:line 31

Why is this happening?

 

And one more thing. In this example at Step 2: Get an authentication access token on next line:

string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

opens window where you need to enter email and password. Is there any way not to show this window and enter email/password in or through the program? I mean I (and users of my future program too) don't want to enter this data manually.

 

I'll appreciate any help.


 

The 404 error indicates that probably you're not pushing data to the correct dataset. Note that only the datasets created via REST API can push data to. I'd doubt that in your case the getdataset() doesn't return expected datasetid, check my reply in this thread. Just try to change some code in the getdataset() function.

 

                    //Get the first id
                    //datasetId = results["value"][0]["id"];

                    var datasets = results["value"];

                    foreach (var ds in datasets) {

                        //return the id of the first salesmarketing dataset
                        if (ds["name"] == "SalesMarketing") {
                            datasetId = ds["id"];
                            break;
                        }
                    }

 

As to your second question, yes, it is possible. Check the approach and concern in Getting around Oauth2. People want to access the reports in Power BI Service shall have their own accounts.

 

 

"As to your second question, yes, it is possible. Check the approach and concern in Getting around Oauth2."

I wrote as you suggested in your answer. Here is my code:

            string requestURL = "https://login.windows.net/common/oauth2/token";
            HttpWebRequest request = System.Net.WebRequest.Create(requestURL) as System.Net.HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Cache-Control", "no-cache");
            string postData = "client_secret=zmZ%2BC1nx3SXgrBzVOce%2BPb4T6uxrpnmmDPHahzPUhE4%3D&client_id=" + clientID + "&grant_type=password&resource=https%3A%2F%2Fanalysis.windows.net%2Fpowerbi%2Fapi&username=MYUSERNAME%40microsoft.com&password=MYPASSWORD";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;

            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse response = request.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            Stream data = response.GetResponseStream();
            response.Close();

and i get the error:

Unhandled Exception: System.Net.WebException: The remote server returned an erro
r: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at PushDataApp.Program.GetToken() in C:\Users\Oleg\Documents\Visual Studio 20
17\Projects\PushDataApp\PushDataApp\Program.cs:line 90
   at PushDataApp.Program.Main(String[] args) in C:\Users\Oleg\Documents\Visual
Studio 2017\Projects\PushDataApp\PushDataApp\Program.cs:line 24

Line 90 is here: 

WebResponse response = request.GetResponse();

Please, help me figure out how to do it.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors