Microsoft Graph API 向任何请求返回 403
Microsoft Graph API returning 403 to any request
我正在开发一个应用程序,在这一点上,它将检索包含登录用户的 Office 组,并根据该信息执行操作。
我正在使用 oAuth2.0 和 v2.0 令牌端点在没有用户的情况下获得访问权限,并且使用下面的代码,我可以向管理员提供对权限的同意(已应用于新应用程序注册门户 https://apps.dev.microsoft.com/ 上的应用程序权限并出现在 Azure 上的企业应用程序部分 ),向 Azure 请求令牌并接收它,但即使应用了权限和该令牌,我从图表中获取 403 响应代码(权限不足)API 我尝试执行的任何请求。
这些操作的代码如下:
// Request Admin Consent
HttpRequestMessage adminConsentRequest = new HttpRequestMessage(HttpMethod.Get, "https://login.microsoftonline.com/" + TenantId + "/adminconsent?client_id="+ClientId+"&redirect_uri=https%3A%2F%2Flocalhost%3A44369%2FHome%2F");
var adminConsentResponse = await client.SendAsync(adminConsentRequest);
// Request Token
HttpRequestMessage tokenRequest = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/"+TenantId+"/oauth2/v2.0/token") { Content = new FormUrlEncodedContent(tokenRequestPairs) };
var tokenResponse = await client.SendAsync(tokenRequest);
string tokenResponseBody = await tokenResponse.Content.ReadAsStringAsync();
var deserializedTokenResponse = (JObject)JsonConvert.DeserializeObject(tokenResponseBody);
string accessToken = deserializedTokenResponse["access_token"].Value<string>();
// Call Microsoft Graph API
HttpRequestMessage graphRequest = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/memberOf");
graphRequest.Headers.Add("Authorization", "Bearer "+accessToken);
var graphResponse = await client.SendAsync(graphRequest);
string graphResponseBody = await graphResponse.Content.ReadAsStringAsync();
var deserializedGraphResponse = (JObject)JsonConvert.DeserializeObject(graphResponseBody);
Enterprise Application permissions on Azure
APP Registration Portal permissions
有人可以指导我犯的任何错误吗?
使用授权令牌和应用权限,我不明白为什么我会收到 AccessDenied 响应。
我申请权限已经超过48小时了,所以这不是同步问题。
更新: 感谢@juunas,我成功地重新应用了权限,令牌现在显示了在应用程序门户上应用的所有权限(User.Read.All,Directory.Read.All 和 Group.Read.All),但 API 仍然是 returns 403 状态代码 (Authorization_RequestDenied).
我尝试了另一个没有 /me 的端点只是为了确保这不是参考问题,但它也是 returns 403 状态代码。
一件有趣的事情是,正如我所说,该应用程序已在新的应用程序门户上注册,并且出现在 Azure 上的企业应用程序中,但没有出现在我的应用程序注册中,因此我只能更改新应用程序的权限应用程序门户。应该是这样的,因为我用的是新的注册入口?
好的,所以在原始 post 更新几分钟后,端点接受了令牌。
唯一的问题是图表 API 无法识别使用 /me
端点登录的用户的 ID,但我使用 /{group-id}/members
端点绕过了它(就我而言,这不是我想要的,但解决了我的问题)。
感谢@juunas 的帮助!
在评论中讨论后,通过重新同意权限解决了问题,与我的博客 post 中所示类似:https://joonasw.net/view/the-grant-requires-admin-permission(尽管它是为 v1 编写的)。
再次运行管理员同意,需要在授权中添加prompt=admin_consent
URL。
我正在开发一个应用程序,在这一点上,它将检索包含登录用户的 Office 组,并根据该信息执行操作。
我正在使用 oAuth2.0 和 v2.0 令牌端点在没有用户的情况下获得访问权限,并且使用下面的代码,我可以向管理员提供对权限的同意(已应用于新应用程序注册门户 https://apps.dev.microsoft.com/ 上的应用程序权限并出现在 Azure 上的企业应用程序部分 ),向 Azure 请求令牌并接收它,但即使应用了权限和该令牌,我从图表中获取 403 响应代码(权限不足)API 我尝试执行的任何请求。
这些操作的代码如下:
// Request Admin Consent
HttpRequestMessage adminConsentRequest = new HttpRequestMessage(HttpMethod.Get, "https://login.microsoftonline.com/" + TenantId + "/adminconsent?client_id="+ClientId+"&redirect_uri=https%3A%2F%2Flocalhost%3A44369%2FHome%2F");
var adminConsentResponse = await client.SendAsync(adminConsentRequest);
// Request Token
HttpRequestMessage tokenRequest = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/"+TenantId+"/oauth2/v2.0/token") { Content = new FormUrlEncodedContent(tokenRequestPairs) };
var tokenResponse = await client.SendAsync(tokenRequest);
string tokenResponseBody = await tokenResponse.Content.ReadAsStringAsync();
var deserializedTokenResponse = (JObject)JsonConvert.DeserializeObject(tokenResponseBody);
string accessToken = deserializedTokenResponse["access_token"].Value<string>();
// Call Microsoft Graph API
HttpRequestMessage graphRequest = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/memberOf");
graphRequest.Headers.Add("Authorization", "Bearer "+accessToken);
var graphResponse = await client.SendAsync(graphRequest);
string graphResponseBody = await graphResponse.Content.ReadAsStringAsync();
var deserializedGraphResponse = (JObject)JsonConvert.DeserializeObject(graphResponseBody);
Enterprise Application permissions on Azure
APP Registration Portal permissions
有人可以指导我犯的任何错误吗? 使用授权令牌和应用权限,我不明白为什么我会收到 AccessDenied 响应。
我申请权限已经超过48小时了,所以这不是同步问题。
更新: 感谢@juunas,我成功地重新应用了权限,令牌现在显示了在应用程序门户上应用的所有权限(User.Read.All,Directory.Read.All 和 Group.Read.All),但 API 仍然是 returns 403 状态代码 (Authorization_RequestDenied).
我尝试了另一个没有 /me 的端点只是为了确保这不是参考问题,但它也是 returns 403 状态代码。
一件有趣的事情是,正如我所说,该应用程序已在新的应用程序门户上注册,并且出现在 Azure 上的企业应用程序中,但没有出现在我的应用程序注册中,因此我只能更改新应用程序的权限应用程序门户。应该是这样的,因为我用的是新的注册入口?
好的,所以在原始 post 更新几分钟后,端点接受了令牌。
唯一的问题是图表 API 无法识别使用 /me
端点登录的用户的 ID,但我使用 /{group-id}/members
端点绕过了它(就我而言,这不是我想要的,但解决了我的问题)。
感谢@juunas 的帮助!
在评论中讨论后,通过重新同意权限解决了问题,与我的博客 post 中所示类似:https://joonasw.net/view/the-grant-requires-admin-permission(尽管它是为 v1 编写的)。
再次运行管理员同意,需要在授权中添加prompt=admin_consent
URL。