PHP code example of brenoroosevelt / oauth2-govbr

1. Go to this page and download the library: Download brenoroosevelt/oauth2-govbr 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/ */

    

brenoroosevelt / oauth2-govbr example snippets


use BrenoRoosevelt\OAuth2\Client\GovBr;

$govBr = new GovBr([
    'clientId'     => 'XXXXXXXX', // Client ID fornecido pelo GovBr
    'clientSecret' => 'YYYYYYYY', // Senha fornecida pelo provedor GovBr
    'redirectUri'  => "https://seu-app-dominio.com.br/seu-login", // Url de redirecionamento
    'redirectUriLogout'   => "https://seu-app-dominio.com.br/seu-logout"
]);

$urlAutorizacao = $govBr->getAuthorizationUrl();
$state = $this->govBr->getState();
// redirecionar o usuário para a url 

$authorizationCode = $_GET['code'];
$accessToken = 
       $govBr->getAccessToken(
            new AuthorizationCode(), 
            ['code' => $authorizationCode]
       );

$govBrUser = $govBr->getResourceOwner($accessToken);         
$govBrUser->getName();
$govBrUser->getCpf();
$govBrUser->getAvatarUrl();
$govBrUser->getProfile();
$govBrUser->getPhoneNumber();
$govBrUser->phoneNumberVerified();
$govBrUser->getEmail();
$govBrUser->emailVerified();

// Obtendo o avatar do usuário
$avatar = $govBr->getAvatar($govBrUser);
if ($avatar !== null) {
    $avatar->image();
    $avatar->imageBase64();
    $avatar->mimeType();
    $avatar->toHtml(['width' => 60]);
}

$urlLogout = $govBr->getLogoutUrl();
// redirecionar  


$govBr = GovBr::staging([
    'clientId'     => 'XXXXXXXX', // Client ID fornecido pelo GovBr
    'clientSecret' => 'YYYYYYYY', // Senha fornecida pelo provedor GovBr
    'redirectUri'  => "https://seu-app-dominio.com.br/seu-login", // Url de redirecionamento
    'redirectUriLogout'   => "https://seu-app-dominio.com.br/seu-logout"
]);