gpt4 book ai didi

rest - 在 Azure 中获取访问 token 时用户名或密码不正确

转载 作者:行者123 更新时间:2023-12-03 02:55:58 25 4
gpt4 key购买 nike

这是我获取访问 token 的代码:

HttpClient clie = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{directoryID}/oauth2/token";
var body = "resource=https://analysis.windows.net/powerbi/api&client_id=clientID&grant_type=password&username=myADusername&password=myADpassword";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");

var result = await clie.PostAsync(tokenEndpoint, stringContent).ContinueWith<string>((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
});

JObject jobject = JObject.Parse(result);

var token = jobject["access_token"].Value<string>();

我正在使用我的 Azure AD 用户名和密码来获取 token 。我在我的 MVC 应用程序 var credential = new UserPasswordCredential(Username,Password); 中使用了相同的凭据,效果很好。但在这个 .NET core 2.0 应用程序中,它给我一个错误,提示无效授权。用户名或密码无效。这些与我用来获取 token 的凭据相同,授权类型设置为密码。我所做的有什么问题吗?

最佳答案

根据我的测试,演示代码对我有用。如果可能,请尝试在您的 Azure AD 中创建一个新用户,并使用创建的用户再次进行测试。用户名格式为<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d585e485f434c40486d55555503555555" rel="noreferrer noopener nofollow">[email protected]</a>

HttpClient clie = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{tenantId}/oauth2/token";
var body = "resource=https://analysis.windows.net/powerbi/api&client_id={nativeAppClientId}&grant_type=password&<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1f190f18040b070f571f190f18040b070f2a12121212440504070309180519050c1e44090507" rel="noreferrer noopener nofollow">[email protected]</a>&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;

测试结果:

enter image description here

关于rest - 在 Azure 中获取访问 token 时用户名或密码不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com