这是我的代码,基本上所有代码都是从Microsoft文档样本代码复制而来的:
private async static void exec()
{
try
{
var clientId = "soemid here";
var tenantID = "some id here";
string[] scopes = new string[] { "User.Read"};
var email = "[email protected]";
System.Security.SecureString password = new System.Security.SecureString();
password.AppendChar('x');
...
// append whole password here
password.AppendChar('x');
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
User me = await graphClient.Me.Request()
.WithUsernamePassword(email, password)
.GetAsync();
Console.WriteLine(me.Id);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
这个问题是什么也没打印,而是我收到了406错误代码。我该如何解决?
顺便说一句,Microsoft 406的文档中的含义是该服务不支持Accept标头中请求的格式。
对于此问题,我会在您的身边使用相同版本的Microsoft.Graph
(Microsoft.Graph.Beta -Version 0.19.0-preview)对其进行测试。但这对我来说很好用,因此我在下面提供我的代码供您参考。
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Security;
using System.Threading.Tasks;
namespace ConsoleApp22
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var clientId = "xxx";
var tenantID = "xxx";
string[] scopes = new string[] { "User.Read" };
var email = "xxx";
var str = "xxx";
var password = new SecureString();
foreach (char c in str) password.AppendChar(c);
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
User me = await graphClient.Me.Request()
.WithUsernamePassword(email, password)
.GetAsync();
Console.WriteLine(me.Id + "===" + me.DisplayName);
}
}
}
对于您的问题,能否请您创建一个新项目并在上面的屏幕截图中与我安装相同的软件包,然后重试。
顺便说一句,我用.net core 3.1运行了代码
=======================更新=====================
如果无法使main
方法异步,则可以编写如下代码:
或者,如果您的执行代码在另一个类中,则可以将代码编写为:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句