<?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 Trying to get Power BI access token via JS - CORS error? in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Trying-to-get-Power-BI-access-token-via-JS-CORS-error/m-p/577500#M17798</link>
    <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a newbie to the whole Power BI thing. Have been asked by a customer to research the potential for embedding power BI data in our PHP/Javascript web application. I have created myself a Power BI Professional account, created a workspace and added some test reports in there. I have also ued the test C# application to embed that report in a web page. The final piece of the puzzle for me is teo embed the same report in our current application.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;And that's where I'm struggling. Since our app is PHP/JS I am trying to use the latter to make the appropriate API calls to get the report content. I believe this should be possible using the REST APIs (although the majority of examples online are C#). However, I currently can't get past the attempt to get an Access Token &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; I have the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;function getPowerBIAccessToken()
{
	var url = 'https://login.microsoftonline.com/common/oauth2/token';

	var username = "my_username"; // Username of PowerBI "pro" account 
	var password = "xxxxxxx"; // Password of PowerBI "pro" account

	var clientID="my_client_id";  // == power BI app ID...
    var formData = {
      grant_type:'password',
      client_id: clientID,
      resource:'https://analysis.windows.net/powerbi/api',
      scope:'openid',
      username:username,
      password:password
    };

	//
	// Create a CORS request object.
	//
	var corsRequest = createCORSRequest('POST', url);
	if(corsRequest)
	{
		corsRequest.onload = function() {
			var response = corsRequest.responseText;
			alert("Request succeeded, response = '"+response+"'");
		};
		
		corsRequest.onerror = function() {
			alert("There was an error with the request");
		}
		
		corsRequest.send(formData);
	}
	else
	{
		alert("Unable to create CORS request");
	}
}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The createCORSRequest function looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;

  }
  return xhr;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which I got from (&lt;A href="https://www.html5rocks.com/en/tutorials/cors/" target="_blank"&gt;https://www.html5rocks.com/en/tutorials/cors/&lt;/A&gt;). However, when I run this code in Chrome I get the following in the console:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="chrome console cors.PNG" style="width: 600px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/135148iA917CBF8317F69FC/image-size/large?v=v2&amp;amp;px=999" role="button" title="chrome console cors.PNG" alt="chrome console cors.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In case that image is too small, the error is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Access to XMLHttpRequest at 'https://login.microsoftonline.com/common/oauth2/token' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, despite using a request object that should support CORS, I seem to be still getting a CORS error. Although, does the HTTP 400 (Bad Request) error that is also shown suggest that the request has been made?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is anyone able to confirm for me whether what I am trying to achieve here is possible? i.e. embedding Power BI content in a web page using only JS to make the API requests? I find it hard to believe that the MS online login URL does not support CORS (unless I am misunderstanding what's going on, of course).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts, suggestions, pointers would be very welcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Al.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Dec 2018 14:41:03 GMT</pubDate>
    <dc:creator>ahenderson</dc:creator>
    <dc:date>2018-12-03T14:41:03Z</dc:date>
    <item>
      <title>Trying to get Power BI access token via JS - CORS error?</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Trying-to-get-Power-BI-access-token-via-JS-CORS-error/m-p/577500#M17798</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a newbie to the whole Power BI thing. Have been asked by a customer to research the potential for embedding power BI data in our PHP/Javascript web application. I have created myself a Power BI Professional account, created a workspace and added some test reports in there. I have also ued the test C# application to embed that report in a web page. The final piece of the puzzle for me is teo embed the same report in our current application.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;And that's where I'm struggling. Since our app is PHP/JS I am trying to use the latter to make the appropriate API calls to get the report content. I believe this should be possible using the REST APIs (although the majority of examples online are C#). However, I currently can't get past the attempt to get an Access Token &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; I have the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;function getPowerBIAccessToken()
{
	var url = 'https://login.microsoftonline.com/common/oauth2/token';

	var username = "my_username"; // Username of PowerBI "pro" account 
	var password = "xxxxxxx"; // Password of PowerBI "pro" account

	var clientID="my_client_id";  // == power BI app ID...
    var formData = {
      grant_type:'password',
      client_id: clientID,
      resource:'https://analysis.windows.net/powerbi/api',
      scope:'openid',
      username:username,
      password:password
    };

	//
	// Create a CORS request object.
	//
	var corsRequest = createCORSRequest('POST', url);
	if(corsRequest)
	{
		corsRequest.onload = function() {
			var response = corsRequest.responseText;
			alert("Request succeeded, response = '"+response+"'");
		};
		
		corsRequest.onerror = function() {
			alert("There was an error with the request");
		}
		
		corsRequest.send(formData);
	}
	else
	{
		alert("Unable to create CORS request");
	}
}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The createCORSRequest function looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;

  }
  return xhr;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which I got from (&lt;A href="https://www.html5rocks.com/en/tutorials/cors/" target="_blank"&gt;https://www.html5rocks.com/en/tutorials/cors/&lt;/A&gt;). However, when I run this code in Chrome I get the following in the console:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="chrome console cors.PNG" style="width: 600px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/135148iA917CBF8317F69FC/image-size/large?v=v2&amp;amp;px=999" role="button" title="chrome console cors.PNG" alt="chrome console cors.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In case that image is too small, the error is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Access to XMLHttpRequest at 'https://login.microsoftonline.com/common/oauth2/token' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, despite using a request object that should support CORS, I seem to be still getting a CORS error. Although, does the HTTP 400 (Bad Request) error that is also shown suggest that the request has been made?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is anyone able to confirm for me whether what I am trying to achieve here is possible? i.e. embedding Power BI content in a web page using only JS to make the API requests? I find it hard to believe that the MS online login URL does not support CORS (unless I am misunderstanding what's going on, of course).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts, suggestions, pointers would be very welcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Al.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 14:41:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Trying-to-get-Power-BI-access-token-via-JS-CORS-error/m-p/577500#M17798</guid>
      <dc:creator>ahenderson</dc:creator>
      <dc:date>2018-12-03T14:41:03Z</dc:date>
    </item>
  </channel>
</rss>

