gpt4 book ai didi

azure - 找不到段 'me' 的资源

转载 作者:行者123 更新时间:2023-12-02 23:46:03 25 4
gpt4 key购买 nike

我正在使用图形 API 检索当前从 Azure AD 登录的用户的个人资料信息,不幸的是我收到以下错误消息:{"odata.error":{"code":"Request_ResourceNotFound","message":{"lang":"en","value":"找不到段“me”的资源。"}} }

下面是我的代码:

Uri serviceRoot = new Uri(serviceRootURL);
ActiveDirectoryClient adClient = new ActiveDirectoryClient(
serviceRoot,
async () => await GetAppTokenAsync());

var user = (User)await adClient.Me
.Expand(x => x.Manager)
.ExecuteAsync();

下面是我的 GetAppTokenAsync() 代码:

private static async Task<string> GetAppTokenAsync()
{
// Instantiate an AuthenticationContext for my directory (see authString above).
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);

// Create a ClientCredential that will be used for authentication.
// This is where the Client ID and Key/Secret from the Azure Management Portal is used.
ClientCredential clientCred = new ClientCredential(clientID, clientSecret);

// Acquire an access token from Azure AD to access the Azure AD Graph (the resource)
// using the Client ID and Key/Secret as credentials.
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred);

// Return the access token.
return authenticationResult.AccessToken;
}

最佳答案

从您的代码“await GetAppTokenAsync()”中,您将获得一个仅应用程序的 token ,它使用应用程序身份,而不是作为用户的身份。如果该 token 未与用户关联,则“(User)await adClient.Me”将不起作用。

使用app token获取用户管理员信息,需要指定要查询的用户,以下代码供您引用:

            try
{
User manager = (User)await adClient.Users.GetByObjectId("5eba8883-c258-45d0-8add-a286a1ec1e91").Manager.ExecuteAsync();
}
catch (Exception ex)
{

throw;
}

更新

您可以使用authorization code flow对于委派权限(用户身份)。如果您想要客户端库代码示例,您可以引用this code sample 。用户登录后,您可以使用以下代码获取当前登录用户的管理员:

            ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
User manager = (User)await client.Me.Manager.ExecuteAsync();

关于azure - 找不到段 'me' 的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670829/

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