Forum Discussion
nunovalente
9 years agoNew Member
Rest API - No GUI
I need to develop an API that when invoked via an endpoint will run an automatic data gathering service and add the results to a PowerBI Streaming Dataset, yet I can't seem to be able to achieve auth...
- 9 years ago
Anonymous
I register a native app but generate the token not via the ADAL library. Instead I'm calling the REST API directly and the token generated could help me to refresh/retrive datasets etc.
static string getAccessTokenSilently() { string resourceUri = "https://analysis.windows.net/powerbi/api"; HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp("https://login.windows.net/common/oauth2/token"); //POST web request to create a datasource. request.KeepAlive = true; request.Method = "POST"; request.ContentLength = 0; request.ContentType = "application/x-www-form-urlencoded"; NameValueCollection parsedQueryString = HttpUtility.ParseQueryString(String.Empty); parsedQueryString.Add("client_id", clientID); parsedQueryString.Add("grant_type", "password"); parsedQueryString.Add("resource", resourceUri); parsedQueryString.Add("username", username); parsedQueryString.Add("password", password); string postdata = parsedQueryString.ToString(); //POST web request byte[] dataByteArray = System.Text.Encoding.ASCII.GetBytes(postdata); ; request.ContentLength = dataByteArray.Length; //Write JSON byte[] into a Stream using (Stream writer = request.GetRequestStream()) { writer.Write(dataByteArray, 0, dataByteArray.Length); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); dynamic responseJson = JsonConvert.DeserializeObject<dynamic>(responseString); return responseJson["access_token"]; } }
Eric_Zhang
9 years agoMicrosoft Employee
Anonymous
I register a native app but generate the token not via the ADAL library. Instead I'm calling the REST API directly and the token generated could help me to refresh/retrive datasets etc.
static string getAccessTokenSilently()
{
string resourceUri = "https://analysis.windows.net/powerbi/api";
HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp("https://login.windows.net/common/oauth2/token");
//POST web request to create a datasource.
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
request.ContentType = "application/x-www-form-urlencoded";
NameValueCollection parsedQueryString = HttpUtility.ParseQueryString(String.Empty);
parsedQueryString.Add("client_id", clientID);
parsedQueryString.Add("grant_type", "password");
parsedQueryString.Add("resource", resourceUri);
parsedQueryString.Add("username", username);
parsedQueryString.Add("password", password);
string postdata = parsedQueryString.ToString();
//POST web request
byte[] dataByteArray = System.Text.Encoding.ASCII.GetBytes(postdata); ;
request.ContentLength = dataByteArray.Length;
//Write JSON byte[] into a Stream
using (Stream writer = request.GetRequestStream())
{
writer.Write(dataByteArray, 0, dataByteArray.Length);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
dynamic responseJson = JsonConvert.DeserializeObject<dynamic>(responseString);
return responseJson["access_token"];
}
}wakhan07
9 years agoFrequent Visitor
Dear Eric_Zhang,
When I use above code to get the token I get The remote server returned an error: (400) Bad Request.
Do you know why this error is thrown?