Can you provider detail about how to fix this issues.
I used below code to get access token and i got it successfully.
protected void Page_Load(object sender, EventArgs e)
{
//Redirect uri must match the redirect_uri used when requesting Authorization code.
string redirectUri = Properties.Settings.Default.RedirectUrl;
string authorityUri = Properties.Settings.Default.AADAuthorityUri;
string resourceUri = Properties.Settings.Default.PowerBiAPI;
string clientId = Properties.Settings.Default.ClientID;
string seceretId = Properties.Settings.Default.ClientSecretKey;
// Get the auth code
string code = Request.Params.GetValues(0)[0];
TokenCache TC = new TokenCache();
AuthenticationContext _authenticationContext = new AuthenticationContext(authorityUri, TC);
ClientCredential cc = new ClientCredential(clientId, seceretId);
AuthenticationResult AR = _authenticationContext.AcquireTokenAsync(resourceUri, cc).Result;
Session["authResult"] = AR;
//Redirect back to Default.aspx
Response.Redirect("/Default.aspx");
}
But when i try to get dataset with below code ,i am getting 403 forbidden error.
protected void getDatasetsButton_Click(object sender, EventArgs e)
{
string responseContent = string.Empty;
//The resource Uri to the Power BI REST API resource
string datasetsUri = Properties.Settings.Default.PowerBiDataset;
//Configure datasets request
System.Net.WebRequest request = System.Net.WebRequest.Create(datasetsUri) as System.Net.HttpWebRequest;
request.Method = "GET";
request.ContentLength = 0;
request.Headers.Add("Authorization", String.Format("Bearer {0}", authResult.AccessToken));
//Get datasets 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();
//Deserialize JSON string
//JavaScriptSerializer class is in System.Web.Script.Serialization
JavaScriptSerializer json = new JavaScriptSerializer();
Datasets datasets = (Datasets)json.Deserialize(responseContent, typeof(Datasets));
resultsTextbox.Text = string.Empty;
//Get each Dataset from
foreach (dataset ds in datasets.value)
{
resultsTextbox.Text += String.Format("{0}\t{1}\n", ds.Id, ds.Name);
}
}
}
}
Error Detail:'request.GetResponse()' threw an exception of type 'System.Net.WebException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: null
InnerException: null
Message: "The remote server returned an error: (403) Forbidden."
Response: {System.Net.HttpWebResponse}
Source: "System"
StackTrace: " at System.Net.HttpWebRequest.GetResponse()"
Status: ProtocolError
TargetSite: {System.Net.WebResponse GetResponse()}
The Token which I got:
"Bearer eyJ0eXAiOiJKV1QiLCJhb.............TH4s-2866kDfaxnGhGU64g"