当我尝试请求组时,MS Graph API返回500

时间:2020-11-10 17:57:47

标签: sharepoint microsoft-graph-api

因此,我正在开发一个共享点Web部件,我需要在其中检查用户是否在AD组中。现在,这是我需要为您提供的MS Graph查询的奇怪部分:

https://graph.microsoft.com/v1.0/me/transitiveMemberOf/microsoft.graph.group?$count=true

这是我的WP发送的确切查询,但是它返回500错误消息。现在我以为我缺少权限,但是资源管理器说我不需要任何权限。

这是我的GraphService,用于处理MS Graph:

import { MSGraphClient } from '@microsoft/sp-http';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import { WebPartContext } from "@microsoft/sp-webpart-base";

/**
 * The class that handles the MS Graph API calls
 */
export default class GraphService {
    /**
     * The MS Graph client with does the calls
     */
    private _client:MSGraphClient;

    /**
     * Sets the client for the Graph API, needs to be called before the class can be used
     * @param context The context of the WP
     */
    public async setClient(context:WebPartContext) {
        this._client = await context.msGraphClientFactory.getClient();
    }
    /**
     * Checks to see if a user belongs in a user group or not
     * @param groupName The group name with we want to check
     */
    public async checkCurrentUserGroup(groupName:string) {
        const rawData = await this._client.api('/me/transitiveMemberOf/microsoft.graph.group').count(true).get();
        const arr = rawData.value.filter(i => i.displayName == groupName);
        return !!arr.length;
    }


    /**
     * Returns the users from AD who have a manager and whom companyName is a specific company name
     * @param companyName The company name to whom the users need to belong to
     */
    public async getUsers(companyName:string):Promise<any[]> {
        const rawData = await this._client.api('/users').filter('accountEnabled eq true').expand('manager').select('displayName,companyName,mail,department,jobTitle').get();

        //need to do manual filtering, becuase you can't user filter on the "companyName" field and you can't search and expand at the same time without returing everything
        const filteredData = rawData.value.filter(i => i.companyName == companyName && i.manager);

        //remaps the data to IUserModel
        const ret:any[] = filteredData.map(i => <any>{
            name: i.displayName,
            id: 0,
            email: i.mail,
            manager: i.manager.displayName,
            managerEmail: i.manager.mail,
            managerId: 0,
            hasDiscussionForm: false,
            department: i.department,
            position: i.jobTitle
        });

        return ret;
        
    } 
}

然后的问题是方法checkCurrentUserGroup返回500。

我已授予wp以下权限:

  • Group.Read.All
  • Directory.Read.All
  • User.Read

getUsers方法可以正常工作。

在MS Graph Explorer中,查询工作正常。我想念什么?

1 个答案:

答案 0 :(得分:1)

根据文档,请求标头必须具有ConsistencyLevel和最终标头,请确保传递请求标头,有关更多信息,请参阅此document

let res = await client.api('/users/{id}/transitiveMemberOf/microsoft.graph.group')
    .header('ConsistencyLevel','eventual')
    .search('displayName:tier')
    .select('displayName,id')
    .orderby('displayName ')
    .get();