At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

, , , , ,

Facebook APIs with HybirdAuth in Laravel

  • By Agira Technologies
  • June 27, 2016
  • 770 Views

Facebook APIs & HybirdAuth

To make any of the web or mobile application more social, one of the top most in list to be integrated is Facebook. Facebook provides a wide variety of APIs for the developers to perform various action. In this blog we are going to explain the usages of HybirdAuth(OAuth 2) method to stream Facebook data with the Laravel code.

How to get Facebook API keys

The following steps illustrate the items to be performed to get the API keys.
Go to https://developers.facebook.com/apps and create a new application by clicking “Create a New App”.
Facebook_APIs_HybirdAuth_Laravel HybirdAuth
Fill out any required fields such as the application name and description.
Create New App ID
Go to the Setting -> Basic. Enter your website domain in the App domains field.
App Settings
Once registered, copy and past the created application credentials (App ID and Secret) into the HybridAuth config file. [Reference: HybridAuth goal is to act as an abstract api between your application and various social apis and identities providers such as Facebook, Twitter and Google]

How to get required information from Facebook Streaming APIs with HybirdAuth in Laravel:

We use HybirdAuth PHP plugin and API Keys to connect to Facebook oAuth API from Laravel.
How to Install HybridAuth:
Execute the following steps to install HybridAuth in Laravel environment.
composer require hybridauth/hybridauth
This commend will automatically download the package, install in the machine and a class namespace will be added exclusively.

How to include configuration data for connecting Facebook:

Create new configuration file inside config folder [~/root].

config/hybirdauth.php
Include the following code in the file.<?phpreturn [
$fbconfig => [
"base_url" => "http://yourwebsite.com/path/to/hybridauth/",
"providers" => [
"Facebook" => [
"enabled" => true,
"keys"    => array (  "id" => "PUT_YOURS_HERE", "secret" => "PUT_YOURS_HERE"  ),
"scope"   => "email, user_about_me, user_birthday, user_hometown", // optional
//"display" => "popup" // optional
]
]
]
]
?>

 
In Controller file (root/app/Http/Controllers/SocialConnectController.php), include the classes with “use”.
use \Hybrid_Auth as Hybridauth;
use \Hybrid_Endpoint as HybridEndpoint;

How to connect Facebook from controller (SocialConnectController.php)

The following code will be useful to connect the Facebook with the authentication API keys from the controller:

use \Hybrid_Auth as Hybridauth;use \Hybrid_Endpoint as HybridEndpoint;
class SocialConnectController extends \BaseController {
public function getFacebookLogin()
{
try
{
$config = Config::get('hybirdauth.fbconfig');
$hybridAuth = new Hybridauth($config);
$provider = $hybridAuth->authenticate('Facebook');
$profile = $provider->getUserProfile();
}
catch(Exception $e)
{
return Redirect::to('facebookAuth');
}
return var_dump($profile).'<br><a href="logout">Logout</a>';
}
public function getCallBack($auth=NULL)
{
HybridEndpoint::process();
return;
}
}

 

How to test the Facebook authentication

Include the following code @ root/app/Http/routes.php
Route::get(‘facebookAuth’, array(“as” => “facebookAuth”, ‘uses’=>’SocialConnectController@getFacebookLogin’));
Route::get(‘callback’, array(“as” => “callback”, ‘uses’=>’SocialConnectController@getSocialProcess’));
Execute the following URL in the browser: http://www.yourwebsite.com/facebookAuth
Now the variable $profile will be having the profile information which is stored in the session. You can use the $profile variable to steam data from the Facebook of the particular user.
– $profile
Information like identifier, username, displayName, firstName, lastName, photoUrl, coverInfoURL, profileURL, webSiteURL, gender, language, description, email, etc… will be fetched as variable values from Facebook profile.
“profile” is an object and you could retrieve the required value as: $profile->[variablename]

Please feel free to ask any queries in the comments section…..
Happy Coding!!!!
For more details reach us at info@agiratech.com

Agira Technologies

AgiraTech is a technology company whose business services and domain solutions supports global clients who comprise the current world economy. Services we offer : Web development, Mobile App development, Blockchain, IoT and DevOps Consulting