Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
tenenwurcel
Regular Visitor

Problems with OAUTH 2.0

Hey everyone,

 

I am trying to get the Power BI acess token, using the Microsoft.IdentityModel.Clients.ActiveDirectory library.

 

I was able to do so, doing the login manually. However my application is going to be used by my costumers who doesn't have access to Power BI Pro, what means that I need to do the login with my credentials, automatically.

 

This is my code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;


namespace ConsoleApp1
{
    class Program
    {
        private static string token = string.Empty;

        static void Main(string[] args)
        {

            GetToken();

        }

        #region Get an authentication access token
        private static async void GetToken()
        {

            string clientID = "myClientID";

            string redirectUri = "myURI";

            string resourceUri = "https://analysis.windows.net/powerbi/api";

            string authorityUri = "https://login.windows.net/common/oauth2/authorize";

            var credential = new UserCredential("myEmail", "myPassword");

            AuthenticationContext authContext = new AuthenticationContext(authorityUri);

            var authenticationResult = await authContext.AcquireTokenAsync(resourceUri, clientID, credential);

            string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

            Console.WriteLine(token);
            Console.ReadLine();

        }

        #endregion

    }

}

 

I am for sure using the right redirectUri, clientID, and credentials, because I am able to login manually.

 

Is my code wrong? If so, could someone help me out with it?

 

Thank you very much, 

 

Henrique.

1 ACCEPTED SOLUTION
Eric_Zhang
Microsoft Employee
Microsoft Employee


@tenenwurcel wrote:

Hey everyone,

 

I am trying to get the Power BI acess token, using the Microsoft.IdentityModel.Clients.ActiveDirectory library.

 

I was able to do so, doing the login manually. However my application is going to be used by my costumers who doesn't have access to Power BI Pro, what means that I need to do the login with my credentials, automatically.

 


@tenenwurcel

In this case, you need to purchase the Power BI Embedded capacity, see Embedding for your customers.

 

As to the code, below demo works in my test.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;  

namespace ConsoleApp1
{
    class Program
    {
        private static string token = string.Empty;

        static void Main(string[] args)
        {

           Task<int> task =   GetToken();
           task.Wait();

           Console.WriteLine(token);
           Console.ReadLine();

        }

        #region Get an authentication access token
        private static async Task<int> GetToken()
        {

            string clientID = "{ClientID}"; 

            string resourceUri = "https://analysis.windows.net/powerbi/api";

            string authorityUri = "https://login.windows.net/common/oauth2/authorize";

            var credential = new UserPasswordCredential("yourAccount", "YourPassword");

            AuthenticationContext authContext = new AuthenticationContext(authorityUri);
             
            // Authenticate using created credentials
            var authenticationContext = new AuthenticationContext(authorityUri);
            var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUri, clientID, credential);
            token = authenticationResult.AccessToken;

            return 1;

        }

        #endregion

    }

}

View solution in original post

1 REPLY 1
Eric_Zhang
Microsoft Employee
Microsoft Employee


@tenenwurcel wrote:

Hey everyone,

 

I am trying to get the Power BI acess token, using the Microsoft.IdentityModel.Clients.ActiveDirectory library.

 

I was able to do so, doing the login manually. However my application is going to be used by my costumers who doesn't have access to Power BI Pro, what means that I need to do the login with my credentials, automatically.

 


@tenenwurcel

In this case, you need to purchase the Power BI Embedded capacity, see Embedding for your customers.

 

As to the code, below demo works in my test.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;  

namespace ConsoleApp1
{
    class Program
    {
        private static string token = string.Empty;

        static void Main(string[] args)
        {

           Task<int> task =   GetToken();
           task.Wait();

           Console.WriteLine(token);
           Console.ReadLine();

        }

        #region Get an authentication access token
        private static async Task<int> GetToken()
        {

            string clientID = "{ClientID}"; 

            string resourceUri = "https://analysis.windows.net/powerbi/api";

            string authorityUri = "https://login.windows.net/common/oauth2/authorize";

            var credential = new UserPasswordCredential("yourAccount", "YourPassword");

            AuthenticationContext authContext = new AuthenticationContext(authorityUri);
             
            // Authenticate using created credentials
            var authenticationContext = new AuthenticationContext(authorityUri);
            var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUri, clientID, credential);
            token = authenticationResult.AccessToken;

            return 1;

        }

        #endregion

    }

}

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.