Resource not found for segment 'me'

I am using the Graph API to get information about the profile of a user who is currently logged in with Azure AD, unfortunately I receive the following error message: {"odata.error": {"code": "Request_ResourceNotFound", "message ": {" lang ":" en "," value ":" No resource found for segment 'me'.}}}

Below is my code:

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

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

      

And below is my code for 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;
        }

      

+3


source to share


1 answer


From your "Wait for GetAppTokenAsync ()" code, you get an application token that uses the application ID, not the user ID. "(User) is waiting for adClient.Me" will not work if this token is not associated with a user.

To use the app token to get the user manager information, you need to specify the user you want to request, the code below is for your reference:

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

                throw;
            }

      



Update

You can use authorization code flow for delegated permissions (user id). If you need a sample client library code, you can refer to this sample code . Once logged in, you can use the below code to get the manager of the current login user:

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

      

0


source







All Articles