My app on Microsoft Teams is using the consent flow to get the permissions from a user. Then with the access token returned by Microsoft, the app is trying to fetch the profile of the user using the /v1.0/me route. But for many users the preferredLanguage field in the profile object is null, only in some users the filed is returning a language. My app needs the preferred language of the user to show a translated version of the app.

I am requesting the following scopes from Microsoft.

  1. User.Read
  2. TeamsActivity.Send (not related to this issue)
  3. offline_access (for refresh token)

Following is the code for the request for fetching the profile.

const { data, status } = await axios({
      method: 'GET',
      url: `https://graph.microsoft.com/v1.0/me?$select=id,mail,displayName,givenName,businessPhones,preferredLanguage`,
      headers: {
        Authorization: `Bearer ${accessToken}`,
        ConsistencyLevel: 'eventual',
      },
});

Below are the screenshots of Microsoft 365 language settings and what the API is returning.

Screenehot of Microsoft 365 language settings where the preferred language set as German Screenshot of console log of the returned profile object where the preferred language is null

1

There are 1 answers

0
Rukmini On BEST ANSWER

I agree with @user2250152, while querying the user details if the preferredLanguage is displaying as null then the user might not have set the preferredLanguage in the Azure AD user details.

Go to Azure Portal -> Users -> Select the user -> Overview -> Properties

enter image description here

Initially I got the preferredLanguage as null while querying the user:

GET https://graph.microsoft.com/v1.0/me?$select=id,mail,displayName,givenName,businessPhones,preferredLanguage

enter image description here

Note that: Some user properties/details cannot be updated directly from the Azure Portal, make use of Microsoft Graph API to update the preferredLanguage property.


PATCH https://graph.microsoft.com/v1.0/me

{
    "preferredLanguage": "en-US"
}

enter image description here

After refreshing the user page, the preferredLanguage property is updated:

enter image description here

Now when I used the same query to fetch the user details, the preferredLanguage is displayed with the value:

GET https://graph.microsoft.com/v1.0/me?$select=id,mail,displayName,givenName,businessPhones,preferredLanguage

enter image description here

{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(id,mail,displayName,givenName,businessPhones,preferredLanguage)/$entity",
"id": "XXX",
"mail": "user@***.onmicrosoft.com",
"displayName": "user",
"givenName": null,
"businessPhones": [],
"preferredLanguage": "en-US"
}