PHP code example of tourze / baidu-oauth2-integrate-bundle

1. Go to this page and download the library: Download tourze/baidu-oauth2-integrate-bundle 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/ */

    

tourze / baidu-oauth2-integrate-bundle example snippets


return [
    // ...
    Tourze\BaiduOauth2IntegrateBundle\BaiduOauth2IntegrateBundle::class => ['all' => true],
];

use Tourze\BaiduOauth2IntegrateBundle\Service\BaiduOAuth2Service;

class AuthController extends AbstractController
{
    public function __construct(
        private BaiduOAuth2Service $oauth2Service
    ) {}

    #[Route('/baidu/login', name: 'baidu_login')]
    public function login(): Response
    {
        $authUrl = $this->oauth2Service->generateAuthorizationUrl();
        return $this->redirect($authUrl);
    }
}

#[Route('/baidu/callback', name: 'baidu_callback')]
public function callback(Request $request): Response
{
    $code = $request->query->get('code');
    $state = $request->query->get('state');

    try {
        $user = $this->oauth2Service->handleCallback($code, $state);
        // Handle user login logic
        return $this->redirectToRoute('dashboard');
    } catch (BaiduOAuth2Exception $e) {
        // Handle OAuth2 errors
        return $this->redirectToRoute('login_failed');
    }
}

class BaiduOAuth2Service
{
    // Generate authorization URL
    public function generateAuthorizationUrl(?string $sessionId = null): string

    // Handle authorization callback
    public function handleCallback(string $code, string $state): BaiduOAuth2User

    // Refresh access token
    public function refreshToken(string $refreshToken): array
}

class BaiduApiClient
{
    // Get user information
    public function getUserInfo(string $accessToken): array

    // Refresh token
    public function refreshToken(string $refreshToken, string $clientId, string $clientSecret): array
}

class BaiduOAuth2Config
{
    private ?int $id;                    // Configuration ID
    private string $clientId;            // Baidu API Key
    private string $clientSecret;        // Baidu Secret Key
    private ?string $scope;              // Authorization scope
    private bool $valid;                 // Is enabled
    private \DateTime $createdAt;        // Created time
    private \DateTime $updatedAt;        // Updated time
}

class BaiduOAuth2User
{
    private ?int $id;                    // User ID
    private string $openid;              // Baidu OpenID
    private ?string $unionid;            // Baidu UnionID
    private ?string $accessToken;        // Access token
    private ?string $refreshToken;       // Refresh token
    private ?\DateTime $tokenExpiresAt;  // Token expiration time
    private ?array $userInfo;            // User information
    private \DateTime $createdAt;        // Created time
    private \DateTime $updatedAt;        // Updated time
}

class BaiduOAuth2State
{
    private ?int $id;                    // State ID
    private string $state;               // State token
    private ?string $sessionId;          // Session ID
    private bool $used;                  // Is used
    private \DateTime $expiresAt;        // Expiration time
    private BaiduOAuth2Config $config;   // Associated configuration
    private \DateTime $createdAt;        // Created time
    private \DateTime $updatedAt;        // Updated time
}

use Tourze\BaiduOauth2IntegrateBundle\Exception\BaiduOAuth2Exception;

// Catch OAuth2 related errors
try {
    $user = $oauth2Service->handleCallback($code, $state);
} catch (BaiduOAuth2Exception $e) {
    // Handle error
    $this->logger->error('Baidu OAuth2 error: ' . $e->getMessage());
}
bash
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate