AzureAD GraphAPI: 如何获取组名

huangapple go评论62阅读模式
英文:

AzureAD GraphAPI: How to get Group-Name

问题

我尝试使用MS Graph API for Java获取用户的组成员关系。

我的用户是7个组的成员。但我只获取到了组ID(类似UUID),而displayName始终为空。

有没有办法获取组的名称?

User usr = gsc.users(MYUSERID).buildRequest().get();
System.out.println("User: " + usr.displayName);

DirectoryObjectCollectionWithReferencesPage doc = gsc.users(MYUSERID).memberOf().buildRequest().get();
java.util.List<DirectoryObject> lst = doc.getCurrentPage();
for (int i = 0; i < lst.size(); i++) {
    if (lst.get(i) instanceof Group) {
        Group gp = (Group) lst.get(i);
        System.out.println("Member: " + gp + " / " + gp.id + " / " + gp.displayName);
    }
}

输出是:

User: MyTestUser
Member: com.microsoft.graph.models.Group@c514b123 / 83091123-c2ea-4883-8a2b-faab7de7433 / null
...

编辑

这也只返回null。

for (int i = 0; i < lst.size(); i++) {
   System.out.println(((Group)doc.getCurrentPage().get(i)).displayName);
}

编辑

改成memberOfAsGroup仍然返回displayName为null。

GroupCollectionPage doc = gsc.users(this.azureuserid).memberOfAsGroup().buildRequest().get();
java.util.List<Group> lst = doc.getCurrentPage();
for (int i = 0; i < lst.size(); i++) {
    Group gp = lst.get(i);
    System.out.println(i + " Member: " + gp + " / " + gp.id + " / " + gp.displayName + " " + gp.description);
}

希望这些能帮助您解决问题。

英文:

I try to get the group-memberships of a user via MS Graph API for Java.

My user is member in 7 groups. But I only get the group ID (sort of UUID) but displayName is always null.

Any idea how to get the name of the group??

User usr = gsc.users (MYUSERID).buildRequest ().get ();
System.out.println (&quot;User: &quot; + usr.displayName);
		
DirectoryObjectCollectionWithReferencesPage doc = gsc.users (MYUSERID).memberOf ().buildRequest ().get ();
java.util.List &lt;DirectoryObject&gt; lst = doc.getCurrentPage();
for (int i = 0; i &lt; lst.size (); i ++)
{
	if (lst.get (i) instanceof Group)
	{
		Group gp = (Group) lst.get (i);
		System.out.println (&quot;Member: &quot; + gp + &quot; / &quot; + gp.id + &quot; / &quot; + gp.displayName);
	}
}

Output is

User: MyTestUser
Member: com.microsoft.graph.models.Group@c514b123 / 83091123-c2ea-4883-8a2b-faab7de7433 / null
...

EDIT

This also only delivers null.

for (int i = 0; i &lt; lst.size (); i ++)
{
   System.out.println (((Group)doc.getCurrentPage().get(i)).displayName);
}

EDIT

Changed to memberOfAsGroup - still null for displayName.

GroupCollectionPage doc = gsc.users (this.azureuserid).memberOfAsGroup ().buildRequest ().get ();
java.util.List &lt;Group&gt; lst = doc.getCurrentPage ();
for (int i = 0; i &lt; lst.size (); i ++)
{
  Group gp = lst.get (i);
  System.out.println(i + &quot;  Member: &quot; + gp + &quot; / &quot; + gp.id + &quot; / &quot; + gp.displayName + &quot;  &quot; + gp.description);
}

答案1

得分: 0

现在正在运作。在Azure中注册的应用程序需要Groups.ReadAll权限(应用程序权限,而不是委托权限!)。

令人惊讶的是,需要的不是分配的权限,而仅仅是对它的管理员同意 - 即使权限被移除。

不知道这是一个错误还是特性。

英文:

Is working now. Registered app in Azure needs permission for Groups.ReadAll (Application, not Delegate!).

Surprisingly not the assigned permission is needed but only the admin-consent to it - even if the persmission is removed.

Do not know if this is a bug or feature.

huangapple
  • 本文由 发表于 2023年5月10日 12:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定