【问题标题】:Resource not found for the segment 'me'未找到片段“我”的资源
【发布时间】:2017-04-28 02:11:51
【问题描述】:

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

下面是我的代码:

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;
        }

【问题讨论】:

    标签: azure azure-ad-graph-api


    【解决方案1】:

    从您的代码“await GetAppTokenAsync()”中,您将获得一个仅应用程序令牌,它使用应用程序身份,而不是作为用户身份。 如果该令牌未与用户关联,则“(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();
    

    【讨论】:

    • 感谢您的信息,但不幸的是,我尝试了谷歌搜索,但无法找到关于如何获取用户身份令牌而不是应用程序令牌的明确答案。你能给我一些建议吗?我在上面编辑了我的问题以包含我用于 GetAppTokenAsync() 的代码。 TQ :)
    • 我尝试了上面的建议,但它需要管理员权限才能运行。我目前只为应用分配了“登录并读取用户资料”授权,因为我只需要读取当前登录用户的资料信息。
    • @DurairajVeeraSinnaiah,请参考我的更新答案。
    • @DurairajVeeraSinnaiah,有什么更新吗?如果您对代码示例有任何问题,请随时告诉我。
    • 您好,很抱歉回复晚了。我仍然收到相同的错误消息: {"odata.error":{"code":"Request_ResourceNotFound","message":{"lang":"en","value":"Resource not found for the segment 'me '."}}} 我怀疑这可能是由于授予在 Azure AD 中注册的应用程序的授权,目前仅授予以下授权:“登录并读取用户配置文件”我认为这个权限应该足够了让用户获取他/她自己的广告信息,但似乎并非如此。这可能是一个原因吗?
    【解决方案2】:

    我使用具有旧版 Azure Active Directory api 和“Application.ReadWrite.OwnedBy”权限的应用程序标识来解决Resource not found for the segment 'me' 错误。 Microsoft Graph api 中存在相同的权限,但行为并不相同。 More information here.

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多