title: PowerBi - how to authenticate to app.powerbi.com silently
body: <p>I have tried the method outlined in the Microsoft docs</p>
  
  <p>which involves creating an app in Active Directory and then having code something very similar to:</p>
  
  <pre><code>var authContextUrl = "https://login.windows.net/common/oauth2/authorize";
  var authenticationContext = new AuthenticationContext(authContextUrl);
  var redirectUri = "https://dev.powerbi.com/Apps/SignInRedirect";
  var pp = new PlatformParameters(PromptBehavior.Auto);
  var result = authenticationContext.AcquireTokenAsync(PowerBiApiResource, clientId, new Uri(redirectUri), pp).GetAwaiter().GetResult();
  
  if (result == null)
  {
      throw new InvalidOperationException("Failed to obtain the PowerBI API token");
  }
  
  var token = result.AccessToken;
  return token;
  </code></pre>
  
  <p>I got this code working but it always insisted on prompting for a username and password, which is a problem for a function app.</p>
  
  <p>I have also tried the approach in the silent function specified here</p>
  
  <p><a href="https://community.powerbi.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/209371#M6614" rel="nofollow noreferrer">https://community.powerbi.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/209371#M6614</a></p>
  
  <pre><code>   static string getAccessTokenSilently()
      {
  
          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";
  
          //Add token to the request header
          request.Headers.Add("Authorization", String.Format("Bearer {0}", token));
  
          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;
  
          //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&lt;dynamic&gt;(responseString);
              return responseJson["access_token"];
          }
  
  
      }
  </code></pre>
  
  <p>This code doesn't work.</p>
  
  <p>Also this has issues, although I haven't tried it:</p>
  
  <p><a href="https://docs.microsoft.com/en-us/power-bi/developer/get-azuread-access-token" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/power-bi/developer/get-azuread-access-token</a></p>
  
  <p>There doesn't appear to be anything up to date available that works that explains how to do this. Does anyone know how?</p>