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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
xuandungpy
Frequent Visitor

Asp.net error: The remote server returned an error: (403) Forbidden

I create Dataset in asp.net but error: The remote server returned an error: (403) Forbidden. Console is working.

 

    public static string createDataset()
    {
        string rs = "";
        HttpWebRequest request = null;
        //string datasetJson = null;
        //Push data into a Power BI dashboard
        try
        {
            string powerBIDatasetsApiUrl = "https://api.powerbi.com/v1.0/myorg/datasets";
            //POST web request to create a dataset.
            //To create a Dataset in a group, use the Groups uri: https://api.PowerBI.com/v1.0/myorg/groups/{group_id}/datasets

            request = System.Net.WebRequest.Create(powerBIDatasetsApiUrl) as System.Net.HttpWebRequest;
            request.KeepAlive = true;
            request.Method = "POST";

            request.ContentType = "application/json";

            if (String.IsNullOrEmpty(token))
            {
                rs = "token is null. " + "<br/>";
                return rs;
            }

            //Add token to the request header
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

            //Create dataset JSON for POST request
            string datasetJson = "{\"name\": \"CorporationProduct\", \"tables\": " +
                    "[{\"name\": \"Product\", \"columns\": " +
                    "[{ \"name\": \"ProductID\", \"dataType\": \"Int64\"}, " +
                    "{ \"name\": \"Name\", \"dataType\": \"string\"}, " +
                    "{ \"name\": \"Category\", \"dataType\": \"string\"}," +
                    "{ \"name\": \"IsCompete\", \"dataType\": \"bool\"}," +
                    "{ \"name\": \"ManufacturedOn\", \"dataType\": \"DateTime\"}" +
                    "]}]}";
            request.ContentLength = datasetJson.Length;
            //POST web request
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(datasetJson);
            request.ContentLength = byteArray.Length;

            //Write JSON byte[] into a Stream
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(byteArray, 0, byteArray.Length);
                writer.Close();
                var response = (HttpWebResponse)request.GetResponse(); // Error: The remote server returned an error: (403) Forbidden.
                rs += string.Format("Dataset {0}", response.StatusCode.ToString()) + "<br/>";
                rs += "createDataset operation is successfull." + "<br/>";
            }
        }
        catch (Exception ex)
        {
            rs += "createDataset encounters an error: ";
            rs += ex.Message;
        }
        return rs;
    }

2 ACCEPTED SOLUTIONS
Eric_Zhang
Employee
Employee

@xuandungpy

The code snippet is good. As it works in a console, it shall work anywhere else. How you get the token? Try to debug the token value in your asp.net application. A valid token shall make it work.

View solution in original post

@Eric_Zhang

Thank you, I got the wrong token. It is work.

View solution in original post

2 REPLIES 2
Eric_Zhang
Employee
Employee

@xuandungpy

The code snippet is good. As it works in a console, it shall work anywhere else. How you get the token? Try to debug the token value in your asp.net application. A valid token shall make it work.

@Eric_Zhang

Thank you, I got the wrong token. It is work.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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