How to access user information by microsoft graph api.

Ankit Rao 1 Reputation point
2022-06-13T14:03:09.26+00:00

Hello Sir,
I want to get the user detail (user profile, username etc.) by Microsoft graph API without login popup screen.
Please help

Thanks & Regards
Ankit singh rao

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,979 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,130 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Shivam Dhiman 6,071 Reputation points
    2022-06-13T16:04:02.397+00:00

    Hi @Ankit Rao

    You can use this sample PowerShell code to get users. Please replace the marked area with relevant values.

    $clientID = "_ _ _ _ _ _ _"  
    $clientSecret ="_ _ _ _ _ _ _"  
    $tenantDomain ="_ _ _ _ _ _ _"  
    $loginURL = "https://login.microsoft.com"  
    $resource = "https://graph.microsoft.com"  
    $body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}  
    $token = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body  
    $apiUri = 'https://graph.microsoft.com/v1.0/users/'  
    $body = "{}"  
    $users = (Invoke-RestMethod -Headers @{Authorization= "Bearer $($token.access_token)" } -Uri $apiUri -Method Get -ContentType 'application/json')  
    

    For more details please refer to this documentation.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


  2. Zehui Yao_MSFT 5,871 Reputation points
    2022-06-20T08:16:08.757+00:00

    Hi @Ankit Rao ,I tested it locally and it works fine, Please check that you are using the corresponding authentication flow and permissions correctly. As you want no login popup, you should use client credentials flow and application permission User.Read.All or User.ReadWrite.All.

    212854-image.png
    212845-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.