bybrand / oauth2-zohocrm
PHP League OAuth 2.0 客户端的 Zoho CRM 提供商
v1.0.2
2021-03-16 18:59 UTC
Requires
- league/oauth2-client: ^2.6
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-17 02:37:17 UTC
README
此包为 PHP League 的 OAuth 2.0 客户端 提供了 Zoho CRM OAuth 2.0 支持。最初,此模块用于 Bybrand 与 Zoho CRM 的集成。
首先,您可以在 "Zoho 开发者控制台" 中获取客户端 ID 和客户端密钥。完整文档,请参阅 Zoho 文档。
安装
composer require bybrand/oauth2-zohocrm
使用方法
这是获取令牌的说明。然后,将其保存在您的数据库中以供未来请求使用。方法 getResourceOwner
返回您的组织,从 /crm/v2/org
。更多信息请参阅 Zoho CRM 文档 获取组织。
如果您不需要组织数据,则不需要使用方法 getResourceOwner
。
use Bybrand\OAuth2\Client\Provider\ZohoCRM as ProviderZohoCRM;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
$params = $_GET;
$provider = new ProviderZohoCRM([
'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' => [
'ZohoCRM.users.ALL',
'ZohoCRM.org.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 CRM 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 CRM 令牌刷新才会发送。请注意,刷新令牌仅在第一次请求后返回,之后将为 null
。
您可以通过撤销访问来在第二次请求中获取令牌刷新。访问 https://accounts.zoho.com 并导航到已连接的应用程序。
$provider = new ProviderZohoCRM([
'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 CRM API 文档。
测试
bash
$ ./vendor/bin/phpunit
或按组进行单个方法测试。
bash
$ ./vendor/bin/phpunit --group=ZohoCRM.GetResourceOwner
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件。