C#图API调用委托
调用Graph API请求 我正在尝试使用c#和控制台应用程序调用委托调用。这是我目前收到的错误代码:generalException\r\n消息:发送请求时出错。\r\n“ 这是一个试图调用graph api的c#控制台C#图API调用委托,c#,asp.net,microsoft-graph-api,C#,Asp.net,Microsoft Graph Api,调用Graph API请求 我正在尝试使用c#和控制台应用程序调用委托调用。这是我目前收到的错误代码:generalException\r\n消息:发送请求时出错。\r\n“ 这是一个试图调用graph api的c#控制台 static void Main(string[] args) { try { RunAsync().GetAwaiter().GetResult(); }
static void Main(string[] args)
{
try
{
RunAsync().GetAwaiter().GetResult();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static async Task RunAsync()
{
string secretKey = <secretKey>;
string appId = <clientID>;
string tenantId = <tenantID>;
string tokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
List<string> scopeURL = new List<string>();
scopeURL.Add("https://graph.microsoft.com/.default");
Console.WriteLine("Enter a username");
string username = Console.ReadLine();
Console.WriteLine("Enter a password");
var password = GetConsoleSecurePassword();
var httpClient = new HttpClient();
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(appId)
.WithTenantId(tenantId)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopeURL);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
try
{
User me = await graphClient.Me.Request()
.WithUsernamePassword(username, password)
.GetAsync();
var books = await graphClient.BookingBusinesses.Request().GetAsync();
foreach (var book in books)
{
Console.WriteLine(book.DisplayName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static SecureString GetConsoleSecurePassword()
{
SecureString pwd = new SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
pwd.RemoveAt(pwd.Length - 1);
Console.Write("\b \b");
}
else
{
pwd.AppendChar(i.KeyChar);
Console.Write("*");
}
}
return pwd;
}
static void Main(字符串[]args)
{
尝试
{
RunAsync().GetAwaiter().GetResult();
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
专用静态异步任务RunAsync()
{
字符串secretKey=;
字符串appId=;
字符串tenantId=;
字符串标记URL=”https://login.microsoftonline.com/common/oauth2/v2.0/token";
List scopeURL=new List();
scopeURL.Add(“https://graph.microsoft.com/.default");
Console.WriteLine(“输入用户名”);
字符串username=Console.ReadLine();
Console.WriteLine(“输入密码”);
var password=GetConsoleSecurePassword();
var httpClient=新的httpClient();
IPPublicClient应用程序publicClientApplication=PublicClientApplicationBuilder
.Create(appId)
.WithTenantId(tenantId)
.Build();
UsernamePasswordProvider authProvider=新的UsernamePasswordProvider(publicClientApplication,scopeURL);
GraphServiceClient graphClient=新的GraphServiceClient(authProvider);
尝试
{
User me=wait graphClient.me.Request()
.WithUsernamePassword(用户名、密码)
.GetAsync();
var books=await-graphClient.bookingbusiness.Request().GetAsync();
foreach(账簿中的var账簿)
{
Console.WriteLine(book.DisplayName);
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
私有静态SecureString GetConsoleSecurePassword()
{
SecureString pwd=新SecureString();
while(true)
{
ConsoleKeyInfo i=Console.ReadKey(true);
如果(i.Key==ConsoleKey.Enter)
{
打破
}
else if(i.Key==ConsoleKey.Backspace)
{
pwd.RemoveAt(pwd.Length-1);
Console.Write(“\b\b”);
}
其他的
{
pwd.AppendChar(i.KeyChar);
控制台。写(“*”);
}
}
返回pwd;
}
我真的很想打电话给bookingBusiness,但我甚至无法阅读我的用户
这是我的申请许可。我错过了什么?
如所问
Message: An error occurred sending the request.
---> Microsoft.Graph.Auth.AuthenticationException: Code: generalException
Message: Unexpected exception returned from MSAL.
---> MSAL.NetCore.4.19.0.0.MsalServiceException:
ErrorCode: service_not_available
Microsoft.Identity.Client.MsalServiceException: Service is unavailable to process the request
at Microsoft.Identity.Client.Http.HttpManager.ExecuteWithRetryAsync(Uri endpoint, IDictionary`2 headers, HttpContent body, HttpMethod method, ICoreLogger logger, Boolean doNotThrow, Boolean retry)
at Microsoft.Identity.Client.Http.HttpManager.ExecuteWithRetryAsync(Uri endpoint, IDictionary`2 headers, HttpContent body, HttpMethod method, ICoreLogger logger, Boolean doNotThrow, Boolean retry)
at Microsoft.Identity.Client.Http.HttpManager.SendGetAsync(Uri endpoint, IDictionary`2 headers, ICoreLogger logger)
at Microsoft.Identity.Client.WsTrust.WsTrustWebRequestManager.GetMexDocumentAsync(String federationMetadataUrl, RequestContext requestContext)
at Microsoft.Identity.Client.WsTrust.CommonNonInteractiveHandler.PerformWsTrustMexExchangeAsync(String federationMetadataUrl, String cloudAudienceUrn, UserAuthType userAuthType, String username, SecureString password)
at Microsoft.Identity.Client.Internal.Requests.UsernamePasswordRequest.FetchAssertionFromWsTrustAsync()
at Microsoft.Identity.Client.Internal.Requests.UsernamePasswordRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenByUsernamePasswordParameters usernamePasswordParameters, CancellationToken cancellationToken)
at Microsoft.Graph.Auth.UsernamePasswordProvider.GetNewAccessTokenAsync(AuthenticationProviderOption msalAuthProviderOption)
StatusCode: 500
ResponseBody: <?xml version="1.0" encoding="utf-8" ?><s:Value xmlns:s="http://www.w3.org/2003/05/soap-envelope">s:Receiver</s:Value>
Headers: Cache-Control: private
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 28 Sep 2020 12:48:52 GMT
--- End of inner exception stack trace ---
at Microsoft.Graph.Auth.UsernamePasswordProvider.GetNewAccessTokenAsync(AuthenticationProviderOption msalAuthProviderOption)
at Microsoft.Graph.Auth.UsernamePasswordProvider.AuthenticateRequestAsync(HttpRequestMessage httpRequestMessage)
at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.UserRequest.GetAsync(CancellationToken cancellationToken)
at ConsoleGraph.Program.ClientDelegateApp(String clientId, String tenantId, String secretKey, List`1 scopes) in D:\src\ConsoleGraph\ConsoleGraph\Program.cs:line 61
消息:发送请求时出错。
--->Microsoft.Graph.Auth.AuthenticationException:代码:generalException
消息:MSAL返回意外异常。
--->MSAL.NetCore.4.19.0.0.MsalServiceException:
错误代码:服务不可用
Microsoft.Identity.Client.MsalServiceException:服务无法处理请求
位于Microsoft.Identity.Client.Http.HttpManager.ExecuteWithRetryAsync(Uri端点、IDictionary`2头、HttpContent正文、HttpMethod方法、ICoreLogger记录器、布尔doNotThrow、布尔重试)
位于Microsoft.Identity.Client.Http.HttpManager.ExecuteWithRetryAsync(Uri端点、IDictionary`2头、HttpContent正文、HttpMethod方法、ICoreLogger记录器、布尔doNotThrow、布尔重试)
位于Microsoft.Identity.Client.Http.HttpManager.SendGetAsync(Uri端点,IDictionary`2头,ICoreLogger记录器)
位于Microsoft.Identity.Client.WsTrust.WsTrustWebRequestManager.GetMexDocumentAsync(字符串federationMetadataUrl,RequestContext RequestContext)
位于Microsoft.Identity.Client.WsTrust.CommonNonInteractiveHandler.PerformWStrustExexChangeAsync(String federationMetadataUrl、String CloudAudienceUrl、UserAuthType UserAuthType、String用户名、SecureString密码)
在Microsoft.Identity.Client.Internal.Requests.UsernamePasswordRequest.FetchAssertionFromWsTrustAsync()中
位于Microsoft.Identity.Client.Internal.Requests.UsernamePasswordRequest.ExecuteAsync(CancellationToken CancellationToken)
位于Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken CancellationToken)
位于Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters、AcquireTokenByUsernamePasswordParameters usernamePasswordParameters、CancellationTokenCancellationToken)
位于Microsoft.Graph.Auth.UsernamePasswordProvider.GetNewAccessTokenAsync(AuthenticationProviderOption msalAuthProviderOption)
状态代码:500
响应体:s:接收器
标头:缓存控制:专用
服务器:Microsoft IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
日期:2020年9月28日星期一12:48:52 GMT
---内部异常堆栈跟踪的结束---
位于Microsoft.Graph.Auth.UsernamePasswordProvider.GetNewAccessTokenAsync(AuthenticationProviderOption msalAuthProviderOption)
位于Microsoft.Graph.Auth.UsernamePasswordProvider.AuthenticateRequestsAsync(HttpRequestMessage HttpRequestMessage)
位于Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage HttpRequestMessage,CancellationToken CancellationToken)
位于System.Net.Http.HttpClient.FinishSendAsyncBuffered(任务'1 sendTask,HttpRequestMessage请求,取消令牌源cts,布尔处理)
在Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage请求,HttpCompletionOption completionOption,CancellationToken CancellationTok