使用dot Net MS Graph SDK 获取自己的用户信息时出现错误代码406?
Error code 406 when I use dot Net MS Graph SDK to get myself's user info?
这是我的代码,基本上都是从微软文档示例代码中复制过来的:
private async static void exec()
{
try
{
var clientId = "soemid here";
var tenantID = "some id here";
string[] scopes = new string[] { "User.Read"};
var email = "xxxx@xx.com";
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 表示 此服务不支持接受 header 中请求的格式。
这个问题,我在我这边用和你一样的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
方法异步,你可以编写如下代码:
或者如果你的执行代码在另一个class中,你可以将代码写成:
这是我的代码,基本上都是从微软文档示例代码中复制过来的:
private async static void exec()
{
try
{
var clientId = "soemid here";
var tenantID = "some id here";
string[] scopes = new string[] { "User.Read"};
var email = "xxxx@xx.com";
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 表示 此服务不支持接受 header 中请求的格式。
这个问题,我在我这边用和你一样的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
方法异步,你可以编写如下代码:
或者如果你的执行代码在另一个class中,你可以将代码写成: