PHP code example of andrewxtsang / oauth-5-laravel

1. Go to this page and download the library: Download andrewxtsang/oauth-5-laravel 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/ */

    

andrewxtsang / oauth-5-laravel example snippets


'providers' => [
	// ...
	
	'Artdarek\OAuth\OAuthServiceProvider',
]

'aliases' => [
	// ...
	
	'OAuth' => 'Artdarek\OAuth\Facade\OAuth',
]


return [ 
	
	/*
	|--------------------------------------------------------------------------
	| oAuth Config
	|--------------------------------------------------------------------------
	*/

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

	/**
	 * Consumers
	 */
	'consumers' => [

		/**
		 * Facebook
		 */
		'Facebook' => [
		    'client_id'     => '',
		    'client_secret' => '',
		    'scope'         => [],
		],		

	]

];

$fb = \OAuth::consumer('Facebook');

$fb = \OAuth::consumer('Facebook', 'http://url.to.redirect.to');

'Facebook' => [
    'client_id'     => 'Your Facebook client ID',
    'client_secret' => 'Your Facebook Client Secret',
    'scope'         => ['email','read_friendlists','user_online_presence'],
],	


public function loginWithFacebook(Request $request)
{
	// get data from request
	$code = $request->get('code');
	
	// get fb service
	$fb = \OAuth::consumer('Facebook');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( ! is_null($code))
	{
		// This was a callback request from facebook, get the token
		$token = $fb->requestAccessToken($code);
		
		// Send a request with it
		$result = json_decode($fb->request('/me'), true);
		
		$message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array.
		dd($result);
	}
	// if not ask for permission first
	else
	{
		// get fb authorization
		$url = $fb->getAuthorizationUri();
		
		// return to facebook login url
		return redirect((string)$url);
	}
}

'Google' => [
    'client_id'     => 'Your Google client ID',
    'client_secret' => 'Your Google Client Secret',
    'scope'         => ['userinfo_email', 'userinfo_profile'],
],	


public function loginWithGoogle(Request $request)
{
	// get data from request
	$code = $request->get('code');
	
	// get google service
	$googleService = \OAuth::consumer('Google');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( ! is_null($code))
	{
		// This was a callback request from google, get the token
		$token = $googleService->requestAccessToken($code);
		
		// Send a request with it
		$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'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array.
		dd($result);
	}
	// if not ask for permission first
	else
	{
		// get googleService authorization
		$url = $googleService->getAuthorizationUri();
		
		// return to google login url
		return redirect((string)$url);
	}
}

'Twitter' => [
    'client_id'     => 'Your Twitter client ID',
    'client_secret' => 'Your Twitter Client Secret',
    // No scope - oauth1 doesn't need scope
],


public function loginWithTwitter(Request $request)
{
	// get data from request
	$token  = $request->get('oauth_token');
	$verify = $request->get('oauth_verifier');
	
	// get twitter service
	$tw = \OAuth::consumer('Twitter');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( ! is_null($token) && ! is_null($verify))
	{
		// This was a callback request from twitter, get the token
		$token = $tw->requestAccessToken($token, $verify);
		
		// Send a request with it
		$result = json_decode($tw->request('account/verify_credentials.json'), true);
		
		$message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array.
		dd($result);
	}
	// if not ask for permission first
	else
	{
		// get request token
		$reqToken = $tw->requestRequestToken();
		
		// get Authorization Uri sending the request token
		$url = $tw->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);

		// return to twitter login url
		return redirect((string)$url);
	}
}

'Linkedin' => [
    'client_id'     => 'Your Linkedin API ID',
    'client_secret' => 'Your Linkedin API Secret',
],


 public function loginWithLinkedin(Request $request)
 {
	// get data from request
	$code = $request->get('code');

	$linkedinService = \OAuth::consumer('Linkedin');


	if ( ! is_null($code))
	{
		// This was a callback request from linkedin, get the token
		$token = $linkedinService->requestAccessToken($code);

		// Send a request with it. Please note that XML is the default format.
		$result = json_decode($linkedinService->request('/people/~?format=json'), true);

		// Show some of the resultant data
		echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName'];

		//Var_dump
		//display whole array.
		dd($result);

	}
	// if not ask for permission first
	else
	{
		// get linkedinService authorization
		$url = $linkedinService->getAuthorizationUri(['state'=>'DCEEFWF45453sdffef424']);

		// return to linkedin login url
		return redirect((string)$url);
	}
}


'Yahoo' => [
	'client_id'     => 'Your Yahoo API KEY',
	'client_secret' => 'Your Yahoo API Secret',
],


public function loginWithYahoo(Request $request)
{
	// get data from request
    $token  = $request->get('oauth_token');
    $verify = $request->get('oauth_verifier');

    \OAuth::setHttpClient('CurlClient');

    // get yahoo service
    $yh = \OAuth::consumer('Yahoo');

    // if code is provided get user data and sign in
    if ( ! is_null($token) && ! is_null($verify))
    {
		// This was a callback request from yahoo, get the token
		$token = $yh->requestAccessToken($token, $verify);

		$xid = [$token->getExtraParams()];
		$result = json_decode($yh->request('https://social.yahooapis.com/v1/user/' . $xid[0]['xoauth_yahoo_guid'] . '/profile?format=json'), true);

		//Var_dump
		//display whole array.
		dd($result);
    }
    // if not ask for permission first
    else
    {
        // get request token
        $reqToken = $yh->requestRequestToken();

        // get Authorization Uri sending the request token
        $url = $yh->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);

        // return to yahoo login url
        return redirect((string)$url);
    }
}

config/app.php
config/app.php

$ php artisan vendor:publish