gregurco/guzzle-bundle-oauth2-plugin

OAuth2 插件为 Guzzle Bundle,Guzzle Bundle 是一个 PHP HTTP 客户端库和框架,用于构建 RESTful Web 服务客户端

v2.3.0 2024-04-07 14:51 UTC

This package is auto-updated.

Last update: 2024-09-07 15:43:29 UTC


README

Build Status Coverage Status SensioLabsInsight

此插件将 OAuth2 功能集成到 Guzzle Bundle 中,Guzzle Bundle 是用于构建 RESTful Web 服务客户端的捆绑包。

先决条件

安装

要安装此捆绑包,请在命令行上运行以下命令,您将获取来自 Packagist 的最新稳定版本。

composer require gregurco/guzzle-bundle-oauth2-plugin

使用方法

启用捆绑包

src/Kernel.php 中找到以下行

foreach ($contents as $class => $envs) {
    if (isset($envs['all']) || isset($envs[$this->environment])) {
        yield new $class();
    }
}

并替换为

foreach ($contents as $class => $envs) {
    if (isset($envs['all']) || isset($envs[$this->environment])) {
        if ($class === \EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle::class) {
            yield new $class([
                new \Gregurco\Bundle\GuzzleBundleOAuth2Plugin\GuzzleBundleOAuth2Plugin(),
            ]);
        } else {
            yield new $class();
        }
    }
}

基本配置

使用默认授权类型(客户端)

# app/config/config.yml

eight_points_guzzle:
    clients:
        api_payment:
            base_url: "http://api.domain.tld"
            
            options:
                auth: oauth2

            # plugin settings
            plugin:
                oauth2:
                    base_uri:       "https://example.com"
                    token_url:      "/oauth/token"
                    client_id:      "test-client-id"
                    client_secret:  "test-client-secret" # optional
                    scope:          "administration"

使用密码授权类型

# app/config/config.yml

eight_points_guzzle:
    clients:
        api_payment:
            base_url: "http://api.domain.tld"
            
            options:
                auth: oauth2

            # plugin settings
            plugin:
                oauth2:
                    base_uri:       "https://example.com"
                    token_url:      "/oauth/token"
                    client_id:      "test-client-id"
                    username:       "johndoe"
                    password:       "A3ddj3w"
                    scope:          "administration"
                    grant_type:     "Sainsburys\\Guzzle\\Oauth2\\GrantType\\PasswordCredentials"

使用正文中的客户端凭证

# app/config/config.yml

eight_points_guzzle:
    clients:
        api_payment:
            base_url: "http://api.domain.tld"
            
            options:
                auth: oauth2

            # plugin settings
            plugin:
                oauth2:
                    base_uri:       "https://example.com"
                    token_url:      "/oauth/token"
                    client_id:      "test-client-id"
                    scope:          "administration"
                    auth_location:  "body"

选项

有关中间件的更多信息,请参阅此处

许可证

此中间件根据 MIT 许可证授权 - 有关详细信息,请参阅 LICENSE 文件