gpt4 book ai didi

azure - Active Directory 集成应用程序 : Access Token Invalid

转载 作者:行者123 更新时间:2023-12-03 00:30:09 25 4
gpt4 key购买 nike

我正在按照本教程 ( https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-integrating-applications ) 创建 OAUTH 应用程序,以将新用户添加到我的 Azure 云订阅。

如教程中所述,我已经创建了我的应用程序,分配了权限并执行了创建 key 。

我正在做的事情非常“标准”,但仍然不起作用。

让我展示一下步骤:

 https://login.microsoftonline.com/common/oauth2/authorize?client_id=[CLIENTID]&response_type=code&redirect_uri=[REDIRECT_URI]&prompt=admin_consent

登录后我成功转到:

Permission Request

接受后,我进入我的服务,在代码身份验证中编写“最后一步”以检索 token

            var content = new StringContent(
"grant_type=authorization_code" +
"&client_id=" + Connectors.Azure.AzureHelper.ID +
"&client_secret=" + Connectors.Azure.AzureHelper.Secret +
"&code=" + code +
"&resource=" + Connectors.Azure.AzureHelper.ID +
"&redirect_uri=" + Request.Url.AbsoluteUri.Split('?')[0],
Encoding.UTF8, "application/x-www-form-urlencoded");

var resp = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/token", content);
var text = await resp.Content.ReadAsStringAsync();

var token = JsonConvert.DeserializeObject<Connectors.Office365.AuthResp>(text);

在 token.access_token 中,我有一个“格式良好”的 token 。

在 token.Scopes 中,我还有很多“授予的权限”,例如:

 Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All Group.Read.All Group.ReadWrite.All Member.Read.Hidden User.Read User.Read.All User.ReadBasic.All

但是如果我尝试执行最简单的操作,例如:

Error on get users

这就像我正在检索有效 token 但没有任何功能!可能出什么问题了?

在“代码确认”的资源字段中,我输入了我的应用程序的 ID。那是对的吗?我还能尝试什么?

最佳答案

也许我发现出了什么问题。一切都与 token 请求中的“Resource”字段有关。

现在我的登录 URL 指定我想要 https://graph.api.net 的 token :

 var myUrl = "https://login.microsoftonline.com/common/oauth2/authorize?client_id=" + AzureHelper.ID + "&response_type=code&redirect_uri=" + baseUrl + "/Account/OAuth&prompt=admin_consent&resource=" + Uri.EscapeDataString("https://graph.windows.net");

这给了我这个网址:

https://login.microsoftonline.com/common/oauth2/authorize?client_id=[ID]&response_type=code&redirect_uri=[URL]&prompt=admin_consent&resource=https%3A%2F%2Fgraph.windows.net

然后,在代码验证时我请求相同的资源:

            var content = new StringContent(
"grant_type=authorization_code" +
"&client_id=" + Connectors.Azure.AzureHelper.ID +
"&client_secret=" + Connectors.Azure.AzureHelper.Secret +
"&code=" + code +
"&resource=" + Uri.EscapeDataString("https://graph.windows.net")+ //Connectors.Azure.AzureHelper.ID +
"&redirect_uri=" + Request.Url.AbsoluteUri.Split('?')[0],
Encoding.UTF8, "application/x-www-form-urlencoded");

var resp = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/token", content);
var text = await resp.Content.ReadAsStringAsync();

var token = JsonConvert.DeserializeObject<Connectors.Office365.AuthResp>(text);

一切都像魅力一样。

我的具体问题是由于在第一次测试中,我尝试在不进行 URL 编码的情况下放置请求参数。

奇怪的事实是,对于“redirect_uri”,您不需要特定的编码,而“请求”则需要它

关于azure - Active Directory 集成应用程序 : Access Token Invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46140330/

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