問題描述
用 C# 編碼.我正在關注本指南:
Coding in C#. I'm following this guide:
https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/#authenticate-service-principal-with-password---powershell%E2%80%8C%E2%80%8B
但它不起作用,而且它不是 Power BI 特定的,所以我不確定如何將它應用到 Power BI API.
But it is not working and it is not Power BI specific so I'm not sure exactly how to apply it to the Power BI API.
在嘗試連接到 Power BI 時,我收到了 403 Forbidden 響應.
In my attempt to connect to Power BI I am getting a 403 Forbidden response.
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} {1}
", ds.Id, ds.Name);
}
}
}
}
catch (WebException wex)
{
resultsTextbox.Text = wex.Message;
}
}
推薦答案
嘗試更改資源 URI:
Try changing the resource 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 獲取令牌.
You want to acquire a token for the power bi api.
希望有所幫助.
根據 OP 評論編輯更新答案:
這是你需要做的.
在 Azure AD 中創建一個本機應用程序"并獲取該客戶端 ID,不會有任何秘密.
In Azure AD create a "Native App" and get that client ID there will be NO Secret.
確保您擁有來自 Nuget Active Directory 身份驗證庫 2.23.302261847 的最新版本的 ADAL
Make sure you have the latest version of ADAL from Nuget Active Directory Authentication Library 2.23.302261847
您將需要使用此 Acquire Token Overload:
You will need to use this Acquire Token Overload:
authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientID, new UserCredential(<Username>, <Password>));
2016-11-11
ADAL 3.13.7 UserCredentail 不再具有上述定義的構造函數.有一個新的密封類 UserPasswordCredential
ADAL 3.13.7 UserCredentail no longer has a constructor as defined above. There is a new sealed class UserPasswordCredential
public sealed class UserPasswordCredential : UserCredential
哪個構造函數與之前的 UserCredential 對象相匹配
Which has the constructor that matches the former UserCredential object
public UserPasswordCredential(string userName, string password)
您可以通過以下方式獲取令牌:
You can acquire the token by doing this:
authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientID, new UserPasswordCredential(<Username>, <Password>));
這篇關于如何使用非交互式身份驗證連接到 Power BI API?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!