获取 Microsoft 联合帐户的访问令牌
Get access token on Microsoft federated accounts
我正在尝试获取 Power BI 的访问令牌 API。我们的帐户是联合帐户。
我一直在尝试这个,但它一直给我一个错误,提示用户名或密码不正确。要使用资源所有者密码凭据授予流程来获取 Azure AD 的访问令牌,我使用 HttpClient
直接调用 http 请求
HttpClient clie = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{tenant}/oauth2/token";
var body = "resource=https://analysis.windows.net/powerbi/api&client_id={client_id}&grant_type=password&username={username}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
string result = clie.PostAsync(tokenEndpoint, stringContent).ContinueWith((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
}).Result;
这适用于非联合帐户。我如何为联合帐户实施相同的操作?
更容易的方法是利用 MSAL.NET(或 ADAL.NET),这对实现这一目标有很大帮助。参见 https://aka.ms/msal-net-up
scopes = new string[]{ "https://analysis.windows.net/powerbi/api/Dashboard.Read.All"}
result = await app.AcquireTokenByUsernamePasswordAsync(scopes, "joe@contoso.com",
securePassword);
如果您知道您的计算机已加入域或 AAD 则更好,您可以使用集成 Windows 身份验证:https://aka.ms/msal-net-iwa
result = await app.AcquireTokenByIntegratedWindowsAuthAsync(scopes);
请注意,我建议使用 MSAL.NET(而不是 ADAM.NET),因为使用 MSAL/NET/the Azure AD v2.0 端点,PowerBI 可以更好地控制权限范围:
在 https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredAppsPreview
的应用程序注册中查看 API 权限选项卡
我正在尝试获取 Power BI 的访问令牌 API。我们的帐户是联合帐户。
我一直在尝试这个,但它一直给我一个错误,提示用户名或密码不正确。要使用资源所有者密码凭据授予流程来获取 Azure AD 的访问令牌,我使用 HttpClient
直接调用 http 请求HttpClient clie = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{tenant}/oauth2/token";
var body = "resource=https://analysis.windows.net/powerbi/api&client_id={client_id}&grant_type=password&username={username}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
string result = clie.PostAsync(tokenEndpoint, stringContent).ContinueWith((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
}).Result;
这适用于非联合帐户。我如何为联合帐户实施相同的操作?
更容易的方法是利用 MSAL.NET(或 ADAL.NET),这对实现这一目标有很大帮助。参见 https://aka.ms/msal-net-up
scopes = new string[]{ "https://analysis.windows.net/powerbi/api/Dashboard.Read.All"}
result = await app.AcquireTokenByUsernamePasswordAsync(scopes, "joe@contoso.com",
securePassword);
如果您知道您的计算机已加入域或 AAD 则更好,您可以使用集成 Windows 身份验证:https://aka.ms/msal-net-iwa
result = await app.AcquireTokenByIntegratedWindowsAuthAsync(scopes);
请注意,我建议使用 MSAL.NET(而不是 ADAM.NET),因为使用 MSAL/NET/the Azure AD v2.0 端点,PowerBI 可以更好地控制权限范围:
在 https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredAppsPreview
的应用程序注册中查看 API 权限选项卡