<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic PowerBi Embedded The remote server returned an error: (400) Bad Request in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/PowerBi-Embedded-The-remote-server-returned-an-error-400-Bad/m-p/668792#M19106</link>
    <description>&lt;P&gt;Below code works fine personal Id, but does&amp;nbsp; not return token when we are using service account which have pro licencese and O365 license as F1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp("&lt;A href="https://login.windows.net/common/oauth2/token" target="_blank"&gt;https://login.windows.net/common/oauth2/token&lt;/A&gt;");&lt;BR /&gt;//POST web request to create a datasource.&lt;BR /&gt;request.KeepAlive = true;&lt;BR /&gt;request.Method = "POST";&lt;BR /&gt;request.ContentLength = 0;&lt;BR /&gt;request.ContentType = "application/x-www-form-urlencoded";&lt;/P&gt;&lt;P&gt;//Add token to the request header&lt;BR /&gt;request.Headers.Add("Authorization", String.Format("Bearer {0}", token));&lt;/P&gt;&lt;P&gt;NameValueCollection parsedQueryString = HttpUtility.ParseQueryString(String.Empty);&lt;BR /&gt;parsedQueryString.Add("client_id", clientID);&lt;BR /&gt;parsedQueryString.Add("grant_type", "password");&lt;BR /&gt;parsedQueryString.Add("resource", resourceUri);&lt;BR /&gt;parsedQueryString.Add("username", username);&lt;BR /&gt;parsedQueryString.Add("password", password);&lt;BR /&gt;string postdata = parsedQueryString.ToString();&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;//POST web request&lt;BR /&gt;byte[] dataByteArray = System.Text.Encoding.ASCII.GetBytes(postdata); ;&lt;BR /&gt;request.ContentLength = dataByteArray.Length;&lt;/P&gt;&lt;P&gt;//Write JSON byte[] into a Stream&lt;BR /&gt;using (Stream writer = request.GetRequestStream())&lt;BR /&gt;{&lt;BR /&gt;writer.Write(dataByteArray, 0, dataByteArray.Length);&lt;BR /&gt;var response = (HttpWebResponse)request.GetResponse();&lt;BR /&gt;var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();&lt;BR /&gt;dynamic responseJson = JsonConvert.DeserializeObject&amp;lt;dynamic&amp;gt;(responseString);&lt;BR /&gt;return responseJson["access_token"];&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when we use SA we get below error;&amp;nbsp;The remote server returned an error: (400) Bad Request; But same SA works fine using below approach...&lt;/P&gt;&lt;P&gt;// Create a user password cradentials.&lt;BR /&gt;var credential = new UserPasswordCredential(Username, Password);&lt;/P&gt;&lt;P&gt;// Authenticate using created credentials&lt;BR /&gt;var authenticationContext = new AuthenticationContext(AuthorityUrl);&lt;BR /&gt;var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, credential);&lt;/P&gt;&lt;P&gt;if (authenticationResult == null)&lt;BR /&gt;{&lt;BR /&gt;result.ErrorMessage = "Authentication Failed.";&lt;BR /&gt;return View(result);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");&lt;/P&gt;</description>
    <pubDate>Fri, 12 Apr 2019 14:31:30 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-04-12T14:31:30Z</dc:date>
    <item>
      <title>PowerBi Embedded The remote server returned an error: (400) Bad Request</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBi-Embedded-The-remote-server-returned-an-error-400-Bad/m-p/668792#M19106</link>
      <description>&lt;P&gt;Below code works fine personal Id, but does&amp;nbsp; not return token when we are using service account which have pro licencese and O365 license as F1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp("&lt;A href="https://login.windows.net/common/oauth2/token" target="_blank"&gt;https://login.windows.net/common/oauth2/token&lt;/A&gt;");&lt;BR /&gt;//POST web request to create a datasource.&lt;BR /&gt;request.KeepAlive = true;&lt;BR /&gt;request.Method = "POST";&lt;BR /&gt;request.ContentLength = 0;&lt;BR /&gt;request.ContentType = "application/x-www-form-urlencoded";&lt;/P&gt;&lt;P&gt;//Add token to the request header&lt;BR /&gt;request.Headers.Add("Authorization", String.Format("Bearer {0}", token));&lt;/P&gt;&lt;P&gt;NameValueCollection parsedQueryString = HttpUtility.ParseQueryString(String.Empty);&lt;BR /&gt;parsedQueryString.Add("client_id", clientID);&lt;BR /&gt;parsedQueryString.Add("grant_type", "password");&lt;BR /&gt;parsedQueryString.Add("resource", resourceUri);&lt;BR /&gt;parsedQueryString.Add("username", username);&lt;BR /&gt;parsedQueryString.Add("password", password);&lt;BR /&gt;string postdata = parsedQueryString.ToString();&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;//POST web request&lt;BR /&gt;byte[] dataByteArray = System.Text.Encoding.ASCII.GetBytes(postdata); ;&lt;BR /&gt;request.ContentLength = dataByteArray.Length;&lt;/P&gt;&lt;P&gt;//Write JSON byte[] into a Stream&lt;BR /&gt;using (Stream writer = request.GetRequestStream())&lt;BR /&gt;{&lt;BR /&gt;writer.Write(dataByteArray, 0, dataByteArray.Length);&lt;BR /&gt;var response = (HttpWebResponse)request.GetResponse();&lt;BR /&gt;var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();&lt;BR /&gt;dynamic responseJson = JsonConvert.DeserializeObject&amp;lt;dynamic&amp;gt;(responseString);&lt;BR /&gt;return responseJson["access_token"];&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when we use SA we get below error;&amp;nbsp;The remote server returned an error: (400) Bad Request; But same SA works fine using below approach...&lt;/P&gt;&lt;P&gt;// Create a user password cradentials.&lt;BR /&gt;var credential = new UserPasswordCredential(Username, Password);&lt;/P&gt;&lt;P&gt;// Authenticate using created credentials&lt;BR /&gt;var authenticationContext = new AuthenticationContext(AuthorityUrl);&lt;BR /&gt;var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, credential);&lt;/P&gt;&lt;P&gt;if (authenticationResult == null)&lt;BR /&gt;{&lt;BR /&gt;result.ErrorMessage = "Authentication Failed.";&lt;BR /&gt;return View(result);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 14:31:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBi-Embedded-The-remote-server-returned-an-error-400-Bad/m-p/668792#M19106</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-04-12T14:31:30Z</dc:date>
    </item>
  </channel>
</rss>

