noardcode/laravel-microsoft-graph

此 Laravel 扩展包为 Microsoft Graph API 提供了一个方便的接口。

v0.0.2 2020-08-07 17:42 UTC

This package is auto-updated.

Last update: 2024-09-08 03:56:32 UTC


README

此 Laravel 扩展包为 Microsoft Graph PHP 库提供了一个便捷的包装器。

Latest Version MIT Licensed Total Downloads

安装

composer require noardcode/laravel-microsoft-graph

发布配置

发布提供的配置文件(config/microsoft-graph.php)。此文件应用于配置您的 Graph API 连接。

php artisan vendor:publish --tag="config"

在 Microsoft 身份平台注册您的应用程序

要连接到 Microsoft Graph API,您首先需要在 Microsoft 身份平台注册您的应用程序。此平台将为您提供所有所需的 IDs 和密钥。

  • 客户端 ID
  • 客户端密钥
  • 租户 ID
  • 对象 ID

您可以通过 Microsoft Azure 门户 注册应用程序。在菜单中选择“Azure Active Directory” > “应用程序注册” > “新建注册”以创建新的注册。

在注册表单中,您需要提供“重定向 URI”。您可以在下面的“xyz”部分找到更多有关此 URI 的信息。

有关应用程序注册的更多信息,请访问 Microsoft 文档

如何使用此包

除了轻松访问几个 Microsoft Graph API 端点外,此包还提供了一个易于使用的 OAuth2.0 包装器,用于授权 API/Microsoft 身份平台。

授权需要两个路由

  • 第一个路由将用户重定向到 Microsoft 身份平台,以便用户同意应用程序请求的权限;
  • 第二个(GET)路由(或 redirect_uri)是在同意后 Microsoft 身份平台重定向到的 URI(webhook)。

以下是一个处理这些操作的示例控制器

<?php

namespace App\Http\Controllers;

use Noardcode\MicrosoftGraph\Requests\AuthorizationRequest;
use Noardcode\MicrosoftGraph\MicrosoftGraphClient;
use Noardcode\MicrosoftGraph\ValueObjects\AuthorizationResponse;

/**
 * Example implementation of the noardcode/laravel-microsoft-graph package.
 *
 * Class Oauth2Controller
 * @package App\Http\Controllers
 */
class Oauth2Controller extends Controller
{
    /**
     * @var MicrosoftGraphClient
     */
    protected MicrosoftGraphClient $client;

    /**
     * This should be a unique/random string that is used to confirm the received request on the webhook
     * belongs to the made authorization request. It must therefore be the same in both methods.
     *
     * @var string
     */
    protected string $state = '1234567890';

    /**
     * Set the client.
     *
     * Oauth2Controller constructor.
     * @param MicrosoftGraphClient $client
     */
    public function __construct(MicrosoftGraphClient $client)
    {
        $this->client = $client;
    }

    /**
     * Redirect the user to the Microsoft identity platform.
     * A method like this could for example be called after clicking a button to connect with Microsoft 365.
     */
    public function getUserConsent(): void
    {
        $this->client->authorize($this->state);
    }

    /**
     * The webhook that is called by the Microsoft identity platform after the user has given his/her consent.
     * The AuthorizationRequest class that is provided by the package immidiatly validates the response.
     *
     * With this response an access token is requested and returned.
     * Save this access token (for example serialized in the databases users table) for later usage when you want to
     * perform requests on the Graph API.
     *
     * @param MicrosoftGraphClient $client
     * @param AuthorizationRequest $request
     */
    public function getToken(MicrosoftGraphClient $client, AuthorizationRequest $request)
    {
        $accessToken = $client->requestAccessToken(
            new AuthorizationResponse($request->all()),
            $this->state
        );

        // Todo: Save the access token, for example by serializing the object (serialize($accessToken)).
    }
}

请确保将正确的重定向 URI 添加到配置或 .env 文件中。

确定权限

权限决定了您的应用程序可以访问哪些资源。当请求同意时,这些权限将显示给用户,并且可以在配置中作为空格分隔的字符串设置。

有关可用权限的更多信息,请参阅 权限参考

发送请求

首先使用检索到的访问令牌作为构造函数参数实例化 MicrosoftGraphClient

$client = new MicrosoftGraphClient($accessToken);

或通过设置器

$client = new MicrosoftGraphClient();
$client->setAccessToken($accessToken);

现在您可以直接获取原始 Microsoft Graph 库

$client->getGraph();

这样,您就可以像使用此包一样访问所有可用端点 描述。使用此包的另一种方法是使用方便的预定义方法,例如

$client->users()->get();

并非所有数据都可通过这些方法访问,但对于可访问的,它们提供了一个易于与 Microsoft Graph API 交互的接口,并返回可用的值对象和/或集合。

目前实现了以下方法

  • users() // 获取登录用户或其他组织用户的信息。

变更日志

请参阅 CHANGELOG 了解最近有哪些更改。

贡献

贡献是 受欢迎的,并且将被完全 归功。我们接受通过Github的Pull Requests进行贡献。

拉取请求

  • PSR-2 编码规范 - 应用约定最简单的方法是安装 PHP Code Sniffer
  • 记录任何行为变更 - 确保更新README.md和任何其他相关文档。
  • 创建功能分支 - 不要要求我们从您的master分支中拉取。
  • 每个功能一个拉取请求 - 如果您想做更多的事情,请发送多个拉取请求。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

参考文献