public ActionResult Index()
{
string AuthorityUrl = @"https://login.windows.net/common/oauth2/authorize";
string ResorceUrl = @"https://analysis.windows.net/powerbi/api";
string APIUrl = @"https://api.powerbi.cn";
string ClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
string Account = "账号";
string Pwd = "密码";
string groupId = "2a8498b7-83d2-4913-af70-111867101fcb";
string datasetId = "2594184";
string result = Result(Account, Pwd, AuthorityUrl, ResorceUrl, APIUrl, ClientId, groupId, datasetId);
ViewBag.msg = result;
return View();
}
private string Result(string Username, string Password, string AuthorityUrl, string ResourceUrl, string ApiUrl, string ClientId, string GroupId, string DatasetId)
{
var credential = new UserPasswordCredential(Username, Password);
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential).Result;
if (authenticationResult == null)
{
return null;
}
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
string res = null;
// Get a list of reports.
HttpWebRequest request;
var url = "https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/refreshes";
request = HttpWebRequest.CreateHttp(string.Format(url, GroupId, DatasetId));
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
request.Headers.Add("Authorization", String.Format("Bearer {0}", authenticationResult.AccessToken));
using (Stream writer = request.GetRequestStream())
{
var response = (HttpWebResponse)request.GetResponse();
res += ("Dataset refresh request{0}", response.StatusCode.ToString());
}
return res;
}
}
相似问题