iamprashant / slack-oauth2

适用于The PHP League OAuth2-Client的Slack OAuth 2.0客户端提供者

v1.2 2023-01-08 19:35 UTC

This package is not auto-updated.

Last update: 2024-09-23 17:32:29 UTC


README

本软件包基于League的OAuth 2.0客户端,使用Slack v2 API。

安装

$ composer require iamprashant/oauth2-slack

用法

用法与The League的OAuth客户端相同,使用\IamPrashant\OAuth2\Client\Provider\Slack作为提供者。

授权URL生成

$provider = new \IamPrashant\OAuth2\Client\Provider\Slack([
    'clientId'          => '{client-id}',
    'clientSecret'      => '{client-secret}',
    'redirectUri'       => '{callback-url}',
]);
 
// to get authorization url
$authUrl = $provider->getAuthorizationUrl();


// if you need to request the user_scope also with global scope
$authUrl = $provider->getAuthorizationUrl(["user_scope"=>"users.profile:read,users:read.email,users:read,im:history"]);

// can request with global scope

$authUrl = $provider->getAuthorizationUrl(['scope' => 'user:read user:write file:write', "user_scope"=>"users.profile:read,users:read.email,users:read,im:history"]);

授权流程

$provider = new \IamPrashant\OAuth2\Client\Provider\Slack([
    'clientId'          => '{client-id}',
    'clientSecret'      => '{client-secret}',
    'redirectUri'       => '{callback-url}',
]);

if (!isset($_GET['code'])) {

    // token requested
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);
    //  Token object will contains channels (if webhoooks enable) and team 
    try {
        // to get user details with complete informaiton
        $user = $provider->getResourceOwner($token);
        // Use these details to create a new profile
        var_dump($user);
    } catch (Exception $e) {
        // Failed to get user details
        exit('something got unexpected');
    }
 
    var_dump($token->getToken());
}

贡献

欢迎创建pull request,感谢您的贡献。