首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用旁路认证接入Power BI API?

如何使用旁路认证接入Power BI API?
EN

Stack Overflow用户
提问于 2016-02-29 14:09:13
回答 2查看 5.3K关注 0票数 3

在C#中编码。我正在遵循这个指南:

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。

在我尝试连接到Power BI时,我得到了一个403禁止响应。

代码语言:javascript
复制
        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;
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2016-03-03 04:22:49

尝试更改资源URI:

代码语言:javascript
复制
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential);

代码语言:javascript
复制
var result = authenticationContext.AcquireToken(resource: **"https://analysis.windows.net/powerbi/api"**, clientCredential: credential);

您想要获取power bi api的令牌。

希望这能有所帮助。

基于OP注释的编辑更新答案:

以下是您需要做的事情。

在Azure AD中,创建一个"Native App“并获取客户端ID,将不会有任何秘密。

确保您拥有Nuget Active Directory身份验证库2.23.302261847提供的最新版本的ADAL

您将需要使用此获取令牌重载:

代码语言:javascript
复制
authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientID, new UserCredential(<Username>, <Password>));

编辑: 2016-11-11

ADAL 3.13.7 UserCredentail不再具有如上定义的构造函数。有一个新的密封类UserPasswordCredential

代码语言:javascript
复制
public sealed class UserPasswordCredential : UserCredential

,它的构造函数与前面的UserCredential对象匹配。

代码语言:javascript
复制
public UserPasswordCredential(string userName, string password)

您可以通过以下方式获取令牌:

代码语言:javascript
复制
authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientID, new UserPasswordCredential(<Username>, <Password>));
票数 3
EN

Stack Overflow用户

发布于 2016-02-29 17:59:33

在PowerBI的网站:https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-what-you-need-to-create-an-app/上有一个学费。

这篇文章详细介绍了如何创建Azure web应用程序,包括在PowerBI AD中注册应用程序和获取ClientID等。这些步骤并不难理解和遵循。如果这是你的需要,请告诉我。如果你有任何问题,请保持联系。

此页面中有一个简单的示例:https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-authenticate-a-web-app/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35692974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档