PHP code example of edisonthk / google-oauth-laravel4

1. Go to this page and download the library: Download edisonthk/google-oauth-laravel4 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/ */

    

edisonthk / google-oauth-laravel4 example snippets


'providers' => array(
	'Edisonthk\GoogleOAuth\GoogleOAuthServiceProvider',
)

 :
 :

'aliases' => array(
	'GoogleOAuth'	=> 'Edisonthk\GoogleOAuth\Facade\GoogleOAuth',
)


 

return array( 
	
	/*
	|--------------------------------------------------------------------------
	| oAuth Config
	|--------------------------------------------------------------------------
	*/

	/**
	 * Storage
	 */
	'storage' => 'Session', 

	/**
	 * Consumers
	 */
	'consumers' => array(

		/**
		 * Google
		 */
        'Google' => array(
            'client_id'     => '',
            'client_secret' => '',
            'redirect_url'	=> '',
            'scope'         => array(),
        ),		

	)

);


$ php artisan config:publish edisonthk/google-oauth-laravel4

'Google' => array(
    'client_id'     => 'Your Google client ID',
    'client_secret' => 'Your Google Client Secret',
    'redirect_url' 	=> 'http://www.example.com/success'
    'scope'         => array('userinfo_email', 'userinfo_profile'),
),



class HomeController extends BaseController {

	public function getIndex()
	{
		$authUrl = GoogleOAuth::getAuthorizationUri();
		$message = "<a href='$authUrl'>Login with Google</a>";

		die($message);
	}

	public function getSuccess()
	{
		$googleService = GoogleOAuth::consumer();

		if(Input::has("code")){
			$code = Input::get("code");
			$googleService->requestAccessToken($code);
			return Redirect::to("/success");
		}

		if(!GoogleOAuth::hasAuthorized()){
			die("Not authorized yet");
		}

        
        $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true );

        $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
        die($message. "<br/>");


	}

	public function getLogout()
	{
		GoogleOAuth::logout();
		return Redirect::to("/");
	}


}