chadhutchins / oauth2-slack
为PHP League OAuth2-Client提供的Slack OAuth 2.0客户端提供者
1.2.2
2020-11-23 15:41 UTC
Requires
- php: >=5.6.0
- league/oauth2-client: 1.*|2.*
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: 5.6
- squizlabs/php_codesniffer: ~2.0
This package is auto-updated.
Last update: 2024-09-23 23:44:20 UTC
README
这是一个对https://github.com/adam-paterson/oauth2-slack的分支,但包含了对Slack v2 API的支持。
OAuth 2.0客户端的Slack提供者
此包为PHP League的OAuth 2.0客户端提供了Slack OAuth 2.0支持。
安装
要安装,请使用composer
$ composer require chadhutchins/oauth2-slack
用法
用法与The League的OAuth客户端相同,使用\Chadhutchins\OAuth2\Client\Provider\Slack
作为提供者。
授权码流
<?php session_start(); $provider = new \Chadhutchins\OAuth2\Client\Provider\Slack([ 'clientId' => '{slack-client-id}', 'clientSecret' => '{slack-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); if (!isset($_GET['code'])) { // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: '.$authUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Optional: Now you have a token you can look up a users profile data try { // We got an access token, let's now get the user's details $team = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $team->getName()); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } // Use this to interact with an API on the users behalf echo $token->getToken(); }
作用域
OAuth作用域,表示您的应用程序想要访问Slack用户账户的哪些部分。完整的作用域列表可以在这里找到。
$provider = new \Chadhutchins\OAuth2\Client\Provider\Slack([ 'clientId' => '{slack-client-id}', 'clientSecret' => '{slack-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); $authUrl = $provider->$provider->getAuthorizationUrl([ 'scope' => 'user:read user:write file:write' ]);
机器人访问令牌
如果您的Slack应用程序包含机器人用户,在批准后,JSON响应将包含一个包含特定于您的机器人用户的访问令牌的附加节点,该令牌在批准的工作空间中使用。
注意:您必须传递bot
作用域,以便此附加节点存在。
$authUrl = $provider->$provider->getAuthorizationUrl([ 'scope' => 'bot' ]); $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); $values = $token->getValues(); // bot user id $botUserId = $values['bot']['bot_user_id']; $botAccessToken = $values['bot']['bot_access_token'];
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅CONTRIBUTING。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。