Forum Discussion
Automate publishing reports process creating an azureDevOps pipeline???
Not sure if this will help, but this is the auth code I use in an Azure Function to log into PowerBi with a service account
TokenCredentials tokenCredentials = new TokenCredentials(await GetUserToken(log), "Bearer");
log.Info(tokenCredentials.CallerId, (string) null);
using (PowerBIClient powerBiClient = new PowerBIClient(new Uri("https://api.powerbi.com/"), (ServiceClientCredentials) tokenCredentials, Array.Empty<DelegatingHandler>()))
{
//code
return req.CreateResponse(HttpStatusCode.OK);
}
public static async Task<string> GetUserToken(TraceWriter log) { string password = Environment.GetEnvironmentVariable("sa_password"); //linked to keyvault string clientId = Environment.GetEnvironmentVariable("clientId"); string user = Environment.GetEnvironmentVariable("sa_user"); AuthenticationContext _authContext = new AuthenticationContext("https://login.microsoftonline.com/<TENANT ID>"); var oauthEndpoint = new Uri("https://login.microsoftonline.com/<TENANT ID>/oauth2/token"); using (var client = new HttpClient()) { var result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api"), new KeyValuePair<string, string>("client_id", clientId), new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", user), new KeyValuePair<string, string>("password", password), new KeyValuePair<string, string>("scope", "openid"), })); var content = await result.Content.ReadAsStringAsync(); var credential = JsonConvert.DeserializeObject<OAuthResult>(content); return credential.AccessToken; } } class OAuthResult { [JsonProperty("token_type")] public string TokenType { get; set; } [JsonProperty("scope")] public string Scope { get; set; } [JsonProperty("experies_in")] public int ExpiresIn { get; set; } [JsonProperty("ext_experies_in")] public int ExtExpiresIn { get; set; } [JsonProperty("experies_on")] public int ExpiresOn { get; set; } [JsonProperty("not_before")] public int NotBefore { get; set; } [JsonProperty("resource")] public Uri Resource { get; set; } [JsonProperty("access_token")] public string AccessToken { get; set; } [JsonProperty("refresh_token")] public string RefreshToken { get; set; } }
Hi everyone, just as an update in case anyone is still interested in this topic,
I have been working with this:
which is basically an agentless job that creates task in a pipeline, this task is used as the name says, to invoke generic rest apis, I have made some basic calls to powerbi's rest api and they work, I haven't had any success with pipeline the task though, this is because the documentation is quite poor and as you may imagine, specific examples are innexistent and being really honest I have zero experience with pipelines/yaml, so, still learning... anyhow I hope someone with more experience than me finds this useful, this is what I've got so far in yaml, I know it's not much, but I hope it serves as a clue for someone. Anyhow, before doing this, you'll need to create a service connection in your devops Project Settings>Service Connections, Create new Generic service Connection