图 returns "Code: ResourceNotFound Message: Invalid version: me Inner error"
Graph returns "Code: ResourceNotFound Message: Invalid version: me Inner error"
我正在尝试执行读取用户配置文件的简单操作。
在我为这个操作授予相关权限后,我可以通过编写以下代码来获取令牌:
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;
}
不幸的是,收到令牌后,此行:await graphClient.Me.Request().GetAsync();
失败并出现以下异常:
Code: ResourceNotFound
Message: Invalid version: me
Inner error
我已经检查了我在 https://jwt.ms/ 中的令牌并验证了 "aud": "https://graph.microsoft.com"
。
问题不在于您的令牌,而在于您 URL,它缺少版本号。
正确查询:
您的查询:
根据文档 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,如果你想使用它
我正在尝试执行读取用户配置文件的简单操作。 在我为这个操作授予相关权限后,我可以通过编写以下代码来获取令牌:
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;
}
不幸的是,收到令牌后,此行:await graphClient.Me.Request().GetAsync();
失败并出现以下异常:
Code: ResourceNotFound
Message: Invalid version: me
Inner error
我已经检查了我在 https://jwt.ms/ 中的令牌并验证了 "aud": "https://graph.microsoft.com"
。
问题不在于您的令牌,而在于您 URL,它缺少版本号。
正确查询:
您的查询:
根据文档 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,如果你想使用它