简体   繁体   English

C# 中的 Microsoft Graph“访问被拒绝。检查凭据并重试”

[英]Microsoft Graph "Access is denied. Check credentials and try again" in C#

This is my code to get emails from my email account through Microsoft Graph, but I keep getting an error regardless on credentials.这是我通过 Microsoft Graph 从我的 email 帐户获取电子邮件的代码,但无论凭据如何,我都会不断收到错误消息。

Code:代码:

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "";
var clientId = "";
var clientSecret = "";

var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var inboxMessages = await graphClient
                        .Users["email"]
                        .MailFolders["inbox"]
                        .Messages
                        .Request()
                        .Expand("attachments")
                        .Top(20)
                        .GetAsync();
Console.WriteLine(inboxMessages);
Console.ReadLine();

Error:错误:

在此处输入图像描述

Permissions:权限:

在此处输入图像描述

In the client credential flow there won't be any user authentication, so delegated permissions doesn't work.在客户端凭证流中,不会有任何用户身份验证,因此委托权限不起作用。 Please give application permissions in the API permissions in azure ad.请在 azure 广告中授予 API 权限中的应用程序权限。

There are two types of permissions.有两种类型的权限。 Application permissions and delegated permissions.应用程序权限和委托权限。 Here if you have a user logging into the app then you would use a different flow and you can use these delegated permissions.在这里,如果您有一个用户登录到应用程序,那么您将使用不同的流程,并且您可以使用这些委派的权限。 But here there is no user as it's a client credential flow.但是这里没有用户,因为它是客户端凭证流。 So use application permissions to make the above code work.所以使用应用程序权限使上面的代码工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问路径被拒绝。 在 c# - Access to the path is denied. in c# RegistrySecurity访问被拒绝。 C# - RegistrySecurity Access is denied. C# 访问路径“C 被拒绝”。 - Access to the path 'C is denied.' C#-UWP-System.UnauthorizedAccessException:“访问被拒绝”。 - C# - UWP - System.UnauthorizedAccessException: 'Access is denied.' C# 对路径的访问被拒绝...(System.UnauthorizedAccessException:对路径'C:\'的访问被拒绝。) - C# access to the path is denied…(System.UnauthorizedAccessException: Access to the path 'C:\' is denied.) 未经授权:访问被拒绝,因为凭证无效。 用于共享点的 Microsoft Graph API - Unauthorized: Access is denied due to invalid credentials . Microsoft Graph API for sharepoint C# 的图表 API:401 - 未经授权:由于凭据无效,访问被拒绝。 在 /planner/plans - Graph API for C#: 401 - Unauthorized: Access is denied due to invalid credentials. on /planner/plans UNC路径中的C#ASP.NET File.OpenRead:“对路径的访问...被拒绝。” - C# ASP.NET File.OpenRead from UNC path: “Access to path…is denied.” SQL Server代理作业 - SSIS - C# - “访问被拒绝。 “当试图删除文件时 - SQL Server Agent Job - SSIS - C# - “Access denied. ” when trying to delete files 使用 IronPDF 将 HTML 转换为 PDF 时出现错误“访问路径 'IronPdf.ChromeRenderingEngine.dll' 被拒绝。” 在 C# 中 - HTML to PDF using ironPDF getting error "access to the path 'IronPdf.ChromeRenderingEngine.dll' is denied." in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM