<?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 Re: Couldn't get token with .Net Core in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/234760#M7398</link>
    <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/39395"&gt;@phathoang&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;May I know the reason why not resolve those packages? Anyway, one way to get data with username and password through pure REST API call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using System;
using System.Net;
//Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
//using Microsoft.IdentityModel.Clients.ActiveDirectory;
//Install-Package Newtonsoft.Json 
using Newtonsoft.Json;
using System.IO;
using System.Web;
using System.Collections.Specialized;

namespace ConsoleApplication39
{

    class Program
    {

        //Step 1 - Replace {client id} with your client app ID. 
        //To learn how to get a client app ID, see Register a client app (https://msdn.microsoft.com/en-US/library/dn877542.aspx#clientID)
        private static string clientID = "client id";

        //RedirectUri you used when you registered your app.
        //For a client app, a redirect uri gives AAD more details on the specific application that it will authenticate.
        private static string redirectUri = "https://login.live.com/oauth20_desktop.srf";

        //Resource Uri for Power BI API
        private static string resourceUri = "https://analysis.windows.net/powerbi/api";

        //OAuth2 authority Uri
        private static string authority = "https://login.windows.net/common/oauth2/authorize";

        //the account used to login Power BI
        private static string username = "pbiaccount";
        private static string password = "pbipassword";

        private static string token = String.Empty;
 

        static void Main(string[] args)
        {

            /**
             the way use Microsoft.IdentityModel.Clients.ActiveDirectory 
            **/
            //getAccessTokenWithLoginPopUp()

            getAccessTokenSilently();
            Console.WriteLine(token);

            Console.ReadKey();

        }


        static void 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;

            try
            {
                //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&amp;lt;dynamic&amp;gt;(responseString);
                    token =  responseJson["access_token"];
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)wex.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            string errorString = reader.ReadToEnd();
                            dynamic respJson = JsonConvert.DeserializeObject&amp;lt;dynamic&amp;gt;(errorString);
                            Console.WriteLine(respJson);
                            //TODO: use JSON.net to parse this string and look at the error message
                        }
                    }
                }
            } 

        }

        /**
        static string getAccessTokenWithLoginPopUp()
        {
            if (token == String.Empty)
            {
                //Get Azure access token
                // Create an instance of TokenCache to cache the access token
                TokenCache TC = new TokenCache();
                // Create an instance of AuthenticationContext to acquire an Azure access token
                authContext = new AuthenticationContext(authority, TC);
                // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
                token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri), PromptBehavior.RefreshSession).AccessToken;
            }
            else
            {
                // Get the token in the cache
                token = authContext.AcquireTokenSilent(resourceUri, clientID).AccessToken;
            }

            return token;
        }
    **/


        }
    }
&lt;/PRE&gt;</description>
    <pubDate>Mon, 21 Aug 2017 02:20:41 GMT</pubDate>
    <dc:creator>Eric_Zhang</dc:creator>
    <dc:date>2017-08-21T02:20:41Z</dc:date>
    <item>
      <title>Couldn't get token with .Net Core</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/233845#M7365</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Is there any way to get token with .Net Core project? I couldn't resolve "Microsoft.PowerBI.Api" and "Microsoft.IdentityModel.Clients.ActiveDirectory" packages in .Net Core project. It requires .net framework.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 06:14:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/233845#M7365</guid>
      <dc:creator>phathoang</dc:creator>
      <dc:date>2017-08-18T06:14:54Z</dc:date>
    </item>
    <item>
      <title>Re: Couldn't get token with .Net Core</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/234760#M7398</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/39395"&gt;@phathoang&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;May I know the reason why not resolve those packages? Anyway, one way to get data with username and password through pure REST API call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using System;
using System.Net;
//Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
//using Microsoft.IdentityModel.Clients.ActiveDirectory;
//Install-Package Newtonsoft.Json 
using Newtonsoft.Json;
using System.IO;
using System.Web;
using System.Collections.Specialized;

namespace ConsoleApplication39
{

    class Program
    {

        //Step 1 - Replace {client id} with your client app ID. 
        //To learn how to get a client app ID, see Register a client app (https://msdn.microsoft.com/en-US/library/dn877542.aspx#clientID)
        private static string clientID = "client id";

        //RedirectUri you used when you registered your app.
        //For a client app, a redirect uri gives AAD more details on the specific application that it will authenticate.
        private static string redirectUri = "https://login.live.com/oauth20_desktop.srf";

        //Resource Uri for Power BI API
        private static string resourceUri = "https://analysis.windows.net/powerbi/api";

        //OAuth2 authority Uri
        private static string authority = "https://login.windows.net/common/oauth2/authorize";

        //the account used to login Power BI
        private static string username = "pbiaccount";
        private static string password = "pbipassword";

        private static string token = String.Empty;
 

        static void Main(string[] args)
        {

            /**
             the way use Microsoft.IdentityModel.Clients.ActiveDirectory 
            **/
            //getAccessTokenWithLoginPopUp()

            getAccessTokenSilently();
            Console.WriteLine(token);

            Console.ReadKey();

        }


        static void 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;

            try
            {
                //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&amp;lt;dynamic&amp;gt;(responseString);
                    token =  responseJson["access_token"];
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)wex.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            string errorString = reader.ReadToEnd();
                            dynamic respJson = JsonConvert.DeserializeObject&amp;lt;dynamic&amp;gt;(errorString);
                            Console.WriteLine(respJson);
                            //TODO: use JSON.net to parse this string and look at the error message
                        }
                    }
                }
            } 

        }

        /**
        static string getAccessTokenWithLoginPopUp()
        {
            if (token == String.Empty)
            {
                //Get Azure access token
                // Create an instance of TokenCache to cache the access token
                TokenCache TC = new TokenCache();
                // Create an instance of AuthenticationContext to acquire an Azure access token
                authContext = new AuthenticationContext(authority, TC);
                // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
                token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri), PromptBehavior.RefreshSession).AccessToken;
            }
            else
            {
                // Get the token in the cache
                token = authContext.AcquireTokenSilent(resourceUri, clientID).AccessToken;
            }

            return token;
        }
    **/


        }
    }
&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Aug 2017 02:20:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/234760#M7398</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-08-21T02:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Couldn't get token with .Net Core</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/236854#M7448</link>
      <description>&lt;P&gt;Thanks for your suggested solution. It works perfectly. I just worry about if PowerBI changes any url or queryParameter when my app is runing. That may make my embedded report stop working any more.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2017 07:48:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Couldn-t-get-token-with-Net-Core/m-p/236854#M7448</guid>
      <dc:creator>phathoang</dc:creator>
      <dc:date>2017-08-23T07:48:36Z</dc:date>
    </item>
  </channel>
</rss>

