Quantcast
Viewing all articles
Browse latest Browse all 17916

API Import 400 Bad Request Error when uploading pbix file

Hello,

 

I have an application where we want to show PowerBI Premium reports in our application.  However, before that happens, we also need to upload PBIX reports using our application.  I have been able to get the proper token, and when I run any get requests (like GetReports), on the API, I get the data back just fine. 

 

However, when I run an Import on the api, it always says 400 Error Bad Request.  I am going to post my code below and would love any help I could get.  I have tried reading in the file from disk, instead of passing it in using FileSelctor tool, I have tried uising different types of requests, made sure I have all permissions assigned to my app through Azure Portal, ect... and nothing has worked. Pretty much I have exausted my thoughts on how to proceed,  It is just strange that every read seems to work great, but a post will not.  

 

All the setup seems to be in place. The only setup I have not done is create Workspace Collections in Azure. The reason for this is it was my understanding that this was part of the old embeded style.  I do have a new AppWorkspace that was created using my Premium account in PowerBI itself.  Not sure if this has something to do with why an import is not working or what, but wanted to give as much info as possible so I can get help ASAP.

 

Thank you all very much, and I look forward to hearing back from you.

 

Steven 

 

 

public async Task Upload([FromUri] int ReportId, [FromUri] string reportName, [FromUri] bool overwrite)
        {
            try
            {

                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                var provider = await Request.Content.ReadAsMultipartAsync<InMemoryMultipartFormDataStreamProvider>(new InMemoryMultipartFormDataStreamProvider());

                //access form data
                NameValueCollection formData = provider.FormData;

                //access files
                IList<HttpContent> files = provider.Files;
                //Example: reading a file's stream like below
                HttpContent file1 = files[0];
               // Stream file1Stream = await file1.ReadAsStreamAsync();

                byte[] fileToSend = await file1.ReadAsByteArrayAsync();

                // Create a user password cradentials.
                var credential = new UserCredential(Username, Password);
                
// Authenticate using created credentials
                var authenticationContext = new AuthenticationContext(AuthorityUrl);
                var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId2, credential);
                token = authenticationResult.AccessToken;
                var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
                
                var nameConflict = "Overwrite";
                string responseContent = string.Empty;

                //Configure dashboards request
                System.Net.WebRequest request = System.Net.WebRequest.Create("https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName=TestImport&nameConflict=Overwrite") as System.Net.HttpWebRequest;
                request.Method = "POST";
// Set the content type of the data being posted.
                request.ContentType = "multipart/form-data";

                // Set the content length of the string being posted.
                request.ContentLength = fileToSend.Length;
                request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(fileToSend, 0, fileToSend.Length);
                    requestStream.Close();
                }

                //Get dashboards response from request.GetResponse()
                using (var response = request.GetResponse() as System.Net.HttpWebResponse)
                {
                    //Get reader from response stream
                    using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        responseContent = reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                var junk = true;
            }
        }
    }

Viewing all articles
Browse latest Browse all 17916

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>