C#中的编码。 我正在遵循这个指南:
但它不起作用而且不是Power BI特定的,因此我不确定如何将其应用于Power BI API。
在我尝试连接到Power BI时,我收到403 Forbidden响应。
var authenticationContext = new AuthenticationContext("https://login.windows.net/" + Properties.Settings.Default.TenantID);
var credential = new ClientCredential(clientId: Properties.Settings.Default.ClientID, clientSecret: Properties.Settings.Default.ClientSecretKey);
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string accessToken = result.AccessToken;
string responseContent = string.Empty;
//The resource Uri to the Power BI REST API resource
string datasetsUri = "https://api.powerbi.com/v1.0/myorg/datasets";
//Configure datasets request
System.Net.WebRequest request = System.Net.WebRequest.Create(datasetsUri) as System.Net.HttpWebRequest;
request.Timeout = 20000;
request.Method = "GET";
request.ContentLength = 0;
request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken));
try
{
//Get datasets response from request.GetResponse()
using (var response = request.GetResponse() as System.Net.HttpWebResponse)
{
//Get reader from response stream
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
{
responseContent = reader.ReadToEnd();
//Deserialize JSON string
//JavaScriptSerializer class is in System.Web.Script.Serialization
JavaScriptSerializer json = new JavaScriptSerializer();
Datasets datasets = (Datasets)json.Deserialize(responseContent, typeof(Datasets));
resultsTextbox.Text = string.Empty;
//Get each Dataset from
foreach (dataset ds in datasets.value)
{
resultsTextbox.Text += String.Format("{0}\t{1}\n", ds.Id, ds.Name);
}
}
}
}
catch (WebException wex)
{
resultsTextbox.Text = wex.Message;
}
}
答案 0 :(得分:3)
尝试更改资源URI:
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential);
到
var result = authenticationContext.AcquireToken(resource: **"https://analysis.windows.net/powerbi/api"**, clientCredential: credential);
您想获得power bi api的令牌。
希望能帮助。
EDIT根据OP评论更新答案:
以下是您需要做的事情。
在Azure AD中创建“本机应用程序”并获取该客户端ID将不会有任何秘密。
确保您拥有Nuget Active Directory身份验证库2.23.302261847的最新版ADAL
您将需要使用此获取令牌重载:
authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientID, new UserCredential(<Username>, <Password>));
编辑:2016-11-11
ADAL 3.13.7 UserCredentail不再具有上面定义的构造函数。有一个新的密封类UserPasswordCredential
public sealed class UserPasswordCredential : UserCredential
其中包含与以前的UserCredential对象匹配的构造函数
public UserPasswordCredential(string userName, string password)
您可以通过以下方式获取令牌:
authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientID, new UserPasswordCredential(<Username>, <Password>));
答案 1 :(得分:0)
PowerBI网站上有一个学费:https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-what-you-need-to-create-an-app/。
本文详细介绍了如何创建PowerBI Web应用程序,包括在Azure AD中注册应用程序以及获取ClientID等。这些步骤不难理解和遵循。如果您需要,请告诉我。如果您有任何问题,请保持联系。
此页面中有一个简单示例:https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-authenticate-a-web-app/