PHP code example of iamprashant / slack-oauth2

1. Go to this page and download the library: Download iamprashant/slack-oauth2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iamprashant / slack-oauth2 example snippets


$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());
}