通过不同字段中的邮箱查询Azure AD用户的Graph API问题
检索包含指定邮箱的用户(mail或otherMails字段)
需要找出所有邮箱存在于mail字段或otherMails数组中的用户,数据结构分为两种情况:
- 邮箱存储在
mail字段:
{ ... mail: "my@email.com", ... }
- 邮箱存储在
otherMails数组:
{ ... otherMails: ["my@email.com"], ... }
使用以下请求URL尝试检索时:
https://graph.windows.net/mytenant.onmicrosoft.com/users?api-version=1.6&$filter=mail eq 'my@email.com' or any(otherMails: mail eq 'my@email.com')
收到错误提示:
"value": "')' or ',' expected at position 48 in 'mail eq 'my@email.com' or any(otherMails: mail eq 'my@email.com')'."
修正方案
错误原因是any函数的语法使用不当,过滤数组字段时,需要明确指代数组内的元素,而非顶层字段。正确的请求URL有两种写法:
写法1:自定义变量指代数组元素
https://graph.windows.net/mytenant.onmicrosoft.com/users?api-version=1.6&$filter=mail eq 'my@email.com' or otherMails/any(m: m eq 'my@email.com')
写法2:用$this指代当前数组元素
https://graph.windows.net/mytenant.onmicrosoft.com/users?api-version=1.6&$filter=mail eq 'my@email.com' or otherMails/any($this eq 'my@email.com')
关键说明
any函数的正确格式是[数组字段名]/any([变量名]: [变量名] eq '目标值'),其中/是数组字段和any函数的必要分隔符- 原请求错误地在
any的条件中引用了顶层的mail字段,而非数组内的元素,同时缺少了/分隔符,导致语法解析失败
内容的提问来源于stack exchange,提问作者coure2011
相关产品推荐
相关产品推荐

