weble / zohoclient
Zoho API 客户端用于OAuth认证和常见任务 - PHP SDK
4.3.0
2024-04-09 13:21 UTC
Requires
- php: ^7.3 || ^8.0
- asad/oauth2-zoho: ^1.0
- league/oauth2-client: ^2.6
- psr/cache: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- cache/array-adapter: ^1.0
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.3
- vlucas/phpdotenv: ^4.1 || ^5.2
README
这个库是一个PHP SDK,简化了Zoho Apis的使用,提供了一个简单的客户端来处理OAuth2的实现,详情请见:[Zoho OAuth协议](https://www.zoho.com/accounts/protocol/oauth.html)
该库旨在为您提供一种简化的方式来生成调用任何所需Zoho API所需的access_token。
安装
composer require weble/zohoclient
示例用法(离线模式)
检索首次对Zoho进行认证的URL,以获取访问令牌/刷新令牌
require_once './vendor/autoload.php'; $client = new \Weble\ZohoClient\OAuthClient('{CLIENT_ID}', '{CLIENT_SECRET}', '{REGION}', '{REDIRECTURL}'); $client->offlineMode(); // this needs to be set if you want to be able to refresh the token $client->promptForConsent(); // Optional setting: Prompts for user consent each time your app tries to access user credentials. // Get the url $client->setScopes([]); // Set the zoho scopes you need, see https://www.zoho.com/crm/developer/docs/api/v2/scopes.html $url = $client->getAuthorizationUrl(); $state = $client->getState(); // Get the state for security, and save it (usually in session) redirect($url); // Do your redirection as you prefer // Wait for the user to redirect... // In the redirection page, check for the state you got before and that you should've stored if ($state !== $_GET['state']) { throw new \Exception('Someone is tampering with the oauth2 request'); } // Try to get an access token (using the authorization code grant) try { $client->setGrantCode($_GET['code']); // if you set the offline mode, you can also get the refresh token here (and store it) $refreshToken = $client->getRefreshToken(); // get the access token (and store it probably) $token = $client->getAccessToken(); } catch (\Exception $e) { // handle your exceptions }
示例用法(在线模式)
require_once './vendor/autoload.php'; $client = new \Weble\ZohoClient\OAuthClient('{CLIENT_ID}', '{CLIENT_SECRET}'); $client->setRegion(\Weble\ZohoClient\Enums\Region::us()); $client->setRedirectUri('{REDIRECT_URI_OF_YOUR_APP}'); $client->onlineMode(); // When you get redirected back to your REDIRECT_URI_OF_YOUR_APP $code = $_GET['code']; $client->setGrantCode($code); // Done! $accessToken = $client->getAccessToken(); // Check if it's expired $isExpired = $client->accessTokenExpired();
模式
Zoho OAuth v2提供了两种主要方式来获取访问令牌
1) 在线
这是在需要让用户使用他们的Zoho帐户进行认证,并代表他们调用API时使用的“标准”方式(即:好像他们已经登录并查询API)。这通常用于以他们的身份登录并自动化某些流程,或者只是需要快速访问他们的个人资料,例如登录/获取他们的名字/获取他们的个人资料。
在线模式最容易实现,但生成的访问令牌会在通常1小时后过期,因此不能存储或续订,除非有刷新令牌,而您使用这种方法不会得到刷新令牌。在令牌过期后,您需要要求用户再次登录。
2) 离线
当您需要自行自主续订访问令牌时,建议使用这种方式。在所有“机器到机器”的通信中使用,并且当您使用API与第三方应用程序(例如您的ERP或电子商务网站)同步时,这是最好的方式。
离线模式生成访问令牌和刷新令牌,您需要将它们**存储**在本地,并在令牌过期时使用它们来刷新访问令牌。
库会自动处理刷新过程,因此您无需担心。
贡献
查找错误、发送拉取请求或改进文档——任何贡献都受欢迎并高度赞赏
版本控制
使用语义版本规范(SemVer)。
版权和许可证
版权所有Weble Srl,受MIT许可证保护。