Java solution please - How to look up a user by personal email address using graph api

Anurag Sinha 21 Reputation points
2022-12-20T18:01:22.443+00:00

I am looking to get the details of any user in Azure B2C by providing the user's email address. I am using Microsoft graph API's in my Java code.
I am trying the following ('email' is a String parameter in the method):
-----
graphClient.users()
.buildRequest()
.filter("mail eq '" + email + "'")
.getAsync());
----
But the response what I get is:
{
"done": false,
"cancelled": false,
"completedExceptionally": false,
"numberOfDependents": 0
}

I am not looking for this response above but the user details - something like:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
"value": [
{
"businessPhones": [],
"displayName": "First Last",
"givenName": null,
"jobTitle": null,
"mail": first.last@keyman .com",
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null,
"surname": null,
"userPrincipalName": "firstLast@keyman .onmicrosoft.com",
"id": "04481a14-934q-416e-cc45-84072250b136"
}
]
}

in my "Java code", I repeat. Everything runs fine on Postman but not in Java code.

Microsoft Security Microsoft Graph
{count} votes

Accepted answer
  1. Anurag Sinha 76 Reputation points
    2022-12-21T15:15:40.443+00:00

    Hi @Zehui Yao_MSFT ,
    Thanks very much for your response. I actually got the solution. I was missing few stuffs which I edged out:

    UserCollectionPage users = graphClient.users()
    .buildRequest()
    .filter("mail eq '" + email + "'")
    .get();

    List<User> currentPage = (users != null) ? users.getCurrentPage() : null;

    Now we can iterate through the list if it's not null. In this case, there can be only one element max in the list as email is unique, but otherwise we can get the users based on whatever filters we use.

    Thanks again,
    Anurag.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.