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.