我正在使用Graph Service访问Microsoft Graph API。然后document通过电子邮件获取用户。我写了一段代码:

var user = null;
const GraphService = require('graph-service');
const ClientCredentials = require('client-credentials');

const tenant = 'my-company.com';
const clientId = '0b13aa29-ca6b-42e8-a083-89e5bccdf141';
const clientSecret = 'lsl2isRe99Flsj32elwe89234ljhasd8239jsad2sl=';

const credentials = new ClientCredentials(tenant, clientId, clientSecret);

const service = new GraphService(credentials);

service.all('/users').then(response => {
      console.log(response.data);
      response.data.forEach(function(item) {
          if (item.userPrincipalName == "lgomez@my-company.com"){
            user = item;
            break;
          }
      });
    });

但是response.data返回一个用户列表,其中包含通过电子邮件获取单个用户对象所需的大量资源。请帮助我更新代码,该代码只能通过电子邮件返回用户的单个对象。

最佳答案

您使用的操作是检索所有用户的列表。
您可以作为单个用户通过调用:
/用户/{id userprincipalname}
此操作接受/usersuserId
例如
/用户/foo@bar.com
[更多信息见documentation]
在您的情况下,代码可以更改为:

//...
var myUPN = "lgomez@my-company.com"
service.get('/users/' + myUPN).then(response => {
  console.log(response.data);
});

10-05 20:48
查看更多