bybrand/oauth2-zohodesk

PHP League OAuth 2.0 客户端的 Zoho Desk 提供商

v1.0.7 2021-01-18 17:28 UTC

This package is auto-updated.

Last update: 2024-09-19 01:39:41 UTC


README

此包为 PHP League 的 OAuth 2.0 客户端 提供Zoho Desk OAuth 2.0 支持。最初,此模块用于 Bybrand 与 Zoho Desk 的集成,并在生产中使用(需要改进)。

首先,您可以在“Zoho 开发者控制台”中获取客户端 ID 和客户端密钥。完整文档,可在Zoho 文档中查看。

安装

composer require bybrand/oauth2-zohodesk

用法

这是一个获取令牌的基础指令,然后将其保存到数据库中以便将来请求。方法 getResourceOwner 返回您的第一个组织,通过 /api/v1/organizations。更多信息请参见 Zoho Desk 文档 获取所有组织

如果您不需要,则不需要获取 getResourceOwner

use Bybrand\OAuth2\Client\Provider\ZohoDesk as ProviderZohoDesk;;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

$params = $_GET;

$provider = new ProviderZohoDesk([
    'clientId'     => 'key-id',
    'clientSecret' => 'secret-key',
    'redirectUri'  => 'your-url-redirect',
]);

if (!empty($params['error'])) {
    // Got an error, probably user denied access
    $message = 'Got error: ' . htmlspecialchars($params['error'], ENT_QUOTES, 'UTF-8');

    // Return error.
    echo $message;
}
if (!isset($params['code']) or empty($params['code'])) {
    // If we don't have an authorization code then get one
    $authorizationUrl = $provider->getAuthorizationUrl([
        'scope' => [
            'Desk.basic.READ',            
        ]
    ]);

    // Get state and store it to the session
    $_SESSION['oauth2state'] = $provider->getState();

    header('Location: '.$authorizationUrl);
    exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($params['state']) || ($params['state'] !== $_SESSION['oauth2state'])) {
    unset($_SESSION['oauth2state']);

    // Set error and redirect.
    echo 'Invalid stage';
} else {
    try {
        // Try to get an access token (using the authorization code grant)
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $params['code']
        ]);

        // Retriave a first Zoho Desk organization.        
        $organization = $provider->getResourceOwner($token);
    } catch (IdentityProviderException $e) {
        // Error, HTTP code Status
    } catch (\Exception $e) {
        // Error, make redirect or message.
    }

    // Save organization data.
    $id   = $organization->getId(),
    $name = $organization->getOrganizationName(),

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}

有关更多信息,请参阅 PHP League 的一般用法示例。

刷新令牌

仅在将 accessType 设置为 offline 时,才会发送 Zoho Desk 令牌刷新。请注意,刷新令牌仅在第一次请求后返回,之后将变为 null

您可以在第二次请求中撤销访问以获取令牌刷新。访问https://accounts.zoho.com 并导航到“连接的应用程序”。

$provider = new ProviderZohoDesk([
    'clientId'     => 'key-id',
    'clientSecret' => 'secret-key',
    'redirectUri'  => 'your-url-redirect',
    'accessType'   => 'offline' // Use only for refresh token.
]);

$token = $provider->getAccessToken('authorization_code', [
    'code' => $code
]);

// Persist the token in a database.
$refreshToken = $token->getRefreshToken();

更多详细信息请参见 从刷新令牌生成访问令牌 Zoho Desk 文档。

测试

bash
$ ./vendor/bin/phpunit

或按组进行单个方法测试。

bash
$ ./vendor/bin/phpunit --group=Zoho.GetResourceOwner

许可

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