简体   繁体   中英

Calling Graph Api from Web Api doesn't return all user data

In my ASP.NET Core Web Api, I'm trying to call Graph API to retrieve the data of other users in the organization. I'm using the On Behalf Of flow. The bootstrap token is passed in from SPA client code.

In the Startup code, I have:

builder.Services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph("https://graph.microsoft.com/beta", "user.read user.readbasic.all profile")
    .AddInMemoryTokenCaches();

In the Controller's Action, I have:

_graphServiceClient.Users.Request()
            .Filter($"mail eq 'someuser@myorg.com'")
            .Select((user) => new {
                DisplayName = user.DisplayName,
                City = user.City,
                Country = user.Country,
                BusinessPhones = user.BusinessPhones
            })
            .Request()
            .GetAsync();

However, at runtime, I only get the DisplayName value. I don't get the values of City, Country or BusinessPhones. They are al null .

When I tried the same query in Graph Explorer, I saw the values for all of them.

What am I missing?

Thank you for reaching out. Please note that beta API's are subject to change. Could you please confirm if you see the same behavior for V1 endpoints as well?

Thanks.

First, your code snippet has an error that you wrote 2 .Request() , you should remove one.

在此处输入图片说明

Whatever flow you used are all generating an access token so that you can access the api, therefore when you got correct data from the api, that means you have a right setting in using the flow, so I think if there's any issue, it should locate at the part of calling api. Then I did some test in my side.

Could you pls check if you call the api directly, you can get the value of City, Country and BUsinessPhones these 3 properties? Per my test, when I call the api, I can get response like screenshot below and by default it's null

https://graph.microsoft.com/beta/users?$filter=mail eq 'tinywang@xxx.onmicrosoft.com'&$select=mail,city,country,DisplayName,BusinessPhones,userType,usageLocation

在此处输入图片说明

And when I followed this tutorial to call the api via sdk, I got the same result:

在此处输入图片说明

My idea is checking the response of calling the api first and if we can't find the issue, then could you pls provide the tutorial you followed to help reproduce the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM