【问题标题】:Creating users using the Microsoft Graph API (PHP)使用 Microsoft Graph API (PHP) 创建用户
【发布时间】:2020-11-30 17:32:09
【问题描述】:

问题

我正在尝试构建一个与Microsoft Graph API 集成的应用程序。

我在 Azure 中有一个管理员帐户,并通过门户设置了一个新应用程序。 我已经下载并安装了PHP SDK,并设法设置了所有内容,以便成功获取用户。

我可以登录应用程序并授予使用我的信息的权限(我请求的权限是Directory.ReadWrite.All,但即使只是请求User.ReadWrite.All 对我不起作用),但是,我的问题似乎是我无法访问其他用户。

以下仅返回我自己的用户:

$graph = new Graph();
$graph->setAccessToken('/* SOMETOKEN */');
$users = $graph->createRequest('GET', '/users')
    ->setReturnType(User::class)
    ->execute();

发布新用户返回 404 错误:

$newUser = new User();
$newUser->setAccountEnabled(true);
$newUser->setGivenName('first_name');
$newUser->setSurname('last_name');
$newUser->setUserPrincipalName('some.email@address.com');

$password = new PasswordProfile();
$password->setPassword('some_password');
$newUser->setPasswordProfile($password);

$user = $graph->createRequest('POST', '/users')
    ->attachBody($newUser)
    ->execute();

返回:

{
    "error": {
        "code": "",
        "message": "No HTTP resource was found that matches the request URI 'https://outlook.office365.com:444/profile/v1.0/users('CID:a8ef4446a149de4d')/profile?api-version=AGSV1-internal'.",
        "innerError": {
            "date": "/* timestamp */",
            "request-id": "/* an id */",
            "client-request-id": "/* an id */"
        }
    }
}

即使尝试使用 Microsoft 的 Graph Explorer 也会遇到同样的错误。

我是否认为这可能是帐户设置问题?

更新

这是 Graph Explorer 返回的错误消息

{
    "error": {
        "code": "",
        "message": "No HTTP resource was found that matches the request URI 'https://outlook.office365.com:444/profile/v1.0/users('CID:a8ef4446a149de4d')/profile?api-version=AGSV1-internal'.",
        "innerError": {
            "date": "2020-11-30T16:51:41",
            "request-id": "743030b4-8835-4a9f-9e3e-d35919a1c289",
            "client-request-id": "c40cd440-d873-ba38-dce7-8669bc561e64"
        }
    }
}

【问题讨论】:

  • 能否提供相关ID和时间戳
  • @SruthiJ-MSFTIdentity by "correlation ID",你的意思是请求 ID 吗?如果没有,我不确定在哪里可以找到它
  • 是的,请在错误信息中提供请求id和时间戳
  • @SruthiJ-MSFTIdentity 我已更新问题以包含此
  • 您能否检查一下 jwt.ms 中的令牌,您需要拥有 User.ReadWrite.All 权限

标签: php microsoft-graph-api microsoft-graph-sdks


【解决方案1】:

我已经解决了。

问题

问题出在权限请求中。 我的应用程序设置为允许个人帐户以及工作/学校帐户。 使用个人帐户登录,我的用户无法授予 *.ReadWrite*.All 权限。

当我从身份验证请求中获取令牌时,它只有 User.Read 权限。

访问所有工作用户所需的步骤

  1. 将 Azure 中的应用更改为仅接受工作/学校帐户
  2. 当我的应用尝试进行身份验证时,我需要使用工作/学校帐户登录
  3. 现在应该可以为User.Read.All 授予权限了
  4. 点击/users 端点应该返回所有用户

为了让 write 工作,我需要 register for a Partner Center MPN ID 并将其与我在 Azure 中的应用程序相关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多