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”.
Fill out any required fields such as the application name and description.
Go to the Setting -> Basic. Enter your website domain in the App domains field.
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].
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:
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.
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…..