public async Task <JsonResult> GetReportList() { var authContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", embedSetting.PbiUserName), new KeyValuePair <string, string>("password", embedSetting.PbiPassword), new KeyValuePair <string, string>("client_id", embedSetting.ApplicationId), new KeyValuePair <string, string>("resource", embedSetting.ResourceUrl) }); var accessToken = await authclient.PostAsync(embedSetting.TokenEndPoint, authContent).ContinueWith <string>((result) => { AzureAdTokenResponseModel tokenRes = JsonConvert.DeserializeObject <AzureAdTokenResponseModel>(result.Result.Content.ReadAsStringAsync().Result); return(tokenRes?.AccessToken); }); // Get PowerBi report url and embed token powerBiClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}"); var response = await powerBiClient.GetAsync($"https://api.powerbi.com/v1.0/myorg/groups/{embedSetting.GroupId}/reports"); string responseData = response.Content.ReadAsStringAsync().Result; var responseObj = JsonConvert.DeserializeObject <PowerBiReportModel>(responseData); List <PowerBiReportModel> listOfReport = new List <PowerBiReportModel>(); listOfReport = responseObj.value; return(Json(listOfReport)); }
public async Task <JsonResult> GetReportByReportId(string reportId) { var accessToken = string.Empty; var authContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", embedSetting.PbiUserName), new KeyValuePair <string, string>("password", embedSetting.PbiPassword), new KeyValuePair <string, string>("client_id", embedSetting.ApplicationId), new KeyValuePair <string, string>("resource", embedSetting.ResourceUrl) }); accessToken = await authclient.PostAsync(embedSetting.TokenEndPoint, authContent).ContinueWith <string>((response) => { AzureAdTokenResponseModel tokenRes = JsonConvert.DeserializeObject <AzureAdTokenResponseModel>(response.Result.Content.ReadAsStringAsync().Result); return(tokenRes?.AccessToken); }); // Get PowerBi report url and embed token powerBiClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}"); var embedUrl = await powerBiClient.GetAsync($"https://api.powerbi.com/v1.0/myorg/groups/{embedSetting.GroupId}/reports/{reportId}") .ContinueWith <string>((response) => { PowerBiReportModel report = JsonConvert.DeserializeObject <PowerBiReportModel>(response.Result.Content.ReadAsStringAsync().Result); return(report?.EmbedUrl); }); var tokenContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("accessLevel", "view") }); var embedToken = await powerBiClient.PostAsync($"https://api.powerbi.com/v1.0/myorg/groups/{embedSetting.GroupId}/reports/{reportId}/GenerateToken", tokenContent) .ContinueWith <string>((response) => { PowerBiEmbedTokenModel powerBiEmbedToken = JsonConvert.DeserializeObject <PowerBiEmbedTokenModel>(response.Result.Content.ReadAsStringAsync().Result); return(powerBiEmbedToken?.Token); }); // JSON Response EmbedContentModel data = new EmbedContentModel { EmbedToken = embedToken, EmbedUrl = embedUrl.Replace("https://app", "https://msit"), }; return(new JsonResult(data)); }
public async Task <string> GetAccessToken() { var authContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", _embedSetting.PbiUserName), new KeyValuePair <string, string>("password", _embedSetting.PbiPassword), new KeyValuePair <string, string>("client_id", _embedSetting.ApplicationId), new KeyValuePair <string, string>("resource", _embedSetting.ResourceUrl) }); var accessToken = await authclient.PostAsync(_embedSetting.TokenEndPoint, authContent).ContinueWith <string>((response) => { AzureAdTokenResponseModel tokenRes = JsonConvert.DeserializeObject <AzureAdTokenResponseModel>(response.Result.Content.ReadAsStringAsync().Result); return(tokenRes?.AccessToken); }); return(accessToken); }