gpt4 book ai didi

c# - 图返回 "Code: ResourceNotFound Message: Invalid version: me Inner error"

转载 作者:行者123 更新时间:2023-12-04 12:21:02 24 4
gpt4 key购买 nike

我正在尝试执行读取用户配置文件的简单操作。
在我授予此操作的相关权限后,我可以通过编写以下代码来获取 token :

static void Main(string[] args)
{
var getToken = new GetTokenEntity()
{
Authority = "https://login.microsoftonline.com/common",
Resource = "https://graph.microsoft.com",
UserName = "myusername",
ClientId = "appclientidguid",
Password = "somepass"
};

var graphClient = new GraphServiceClient("https://graph.microsoft.com", new DelegateAuthenticationProvider(async(requestMessage) =>
{
var authResult = await GetToken(getToken);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
}));

var inbox = GetMessages(graphClient).GetAwaiter().GetResult();
}

public async static Task<AuthenticationResult> GetToken(GetTokenEntity getToken)
{
var authenticationContext = new AuthenticationContext(getToken.Authority);
var authenticationResult = await authenticationContext
.AcquireTokenAsync(getToken.Resource, getToken.ClientId,
new UserPasswordCredential(getToken.UserName, getToken.Password));
return authenticationResult;
}

public async static Task<User> GetMessages(GraphServiceClient graphClient)
{
var currentUser = await graphClient.Me.Request().GetAsync();
return currentUser;
}

不幸的是,收到 token 后,这一行: await graphClient.Me.Request().GetAsync();失败,此异常:
Code: ResourceNotFound
Message: Invalid version: me

Inner error

我已在 https://jwt.ms/ 中检查了我的 token 到并验证 "aud": "https://graph.microsoft.com" .

最佳答案

根据文档 https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md (以及 Visual Studio 中的智能提示)您的基本 URL 应该是

https://graph.microsoft.com/currentServiceVersion

所以这一行
var graphClient = new GraphServiceClient("https://graph.microsoft.com", new DelegateAuthenticationProvider(async (requestMessage) =

应该是
var graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(async (requestMessage) =

或/beta 如果你想使用它

关于c# - 图返回 "Code: ResourceNotFound Message: Invalid version: me Inner error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57676838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com