PHP code example of alexsoft / laravel-socialite-bitbucket

1. Go to this page and download the library: Download alexsoft/laravel-socialite-bitbucket 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/ */

    

alexsoft / laravel-socialite-bitbucket example snippets


'providers' => [
    // Other service providers
    
    Alexsoft\LaravelSocialiteBitbucket\Provider::class,

]

'bitbucket' => [
    'client_id' => env('BITBUCKET_CLIENT_ID'),
    'client_secret' => env('BITBUCKET_CLIENT_SECRET'),
    'redirect' => env('BITBUCKET_REDIRECT'),
],



namespace App\Http\Controllers\Auth;

use Socialite;

class AuthController extends Controller
{
    /**
     * Redirect the user to the Bitbucket authentication page.
     *
     * @return Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('bitbucket2')->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('bitbucket2')->user();

        // $user->token;
    }
}

Route::get('auth/bitbucket', 'Auth\AuthController@redirectToProvider');
Route::get('auth/bitbucket/callback', 'Auth\AuthController@handleProviderCallback');

$user = Socialite::driver('bitbucket2')->user();

// OAuth Two Providers
$token = $user->token;
$refreshToken = $user->refreshToken; // may not always be provided
$expiresIn = $user->expiresIn;

// OAuth One Providers
$token = $user->token;
$tokenSecret = $user->tokenSecret;

// All Providers
$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();

$options = [
    'auth' => [config('services.bitbucket.client_id'), config('services.bitbucket.client_secret')],
    'form_params' => [
        'grant_type' => 'refresh_token',
        'refresh_token' => "BITBUCKET_REFRESH_TOKEN"
    ]
];

$response = (new GuzzleHttp\Client)
    ->post('https://bitbucket.org/site/oauth2/access_token', $options)
    ->getBody()->getContents();

$response = json_decode($response, true);

$newAccessToken = $response['access_token'];