Home / API / Login with Facebook oauth API

Login with Facebook oauth API

Facebook oauth API is a concept where you allow anonymous users to access authorized content using their Facebook account. This kind of login system is what internet users prefer to quickly login to a website instead of filling up registration forms and activate account from a link. After all users prefer easiest methods in any aspect of internet world. In this tutorial you will learn how to authorize a user by redirecting them to facebook and get their account details.

App Setup

1. Login to Apps Dashboard with your facebook account

2. Go to My Apps and create a new Application

3. Go to Settings and update the website URL by clicking on Add Platform -> Website

4. Finally go to Status & Review and make your App Live to public

5. After setting up the App, download the facebook SDK for PHP. You have the copy in the download file. You can find that I have taken only the Facebook folder which contains necessary files for the project.


Let us look into the index page in two steps,

Step 1

Call the method setDefaultApplication of class FacebookSession with APPID and APPSECRET. If session is not setup the redirect method in a variable.

Step 2

Show all the information received in the API response if session is set else simply show a Login button with a link to getLoginUrl(), a method of Facebook PHP SDK.

Response Handling

After successful login the user will be redirected to the URL mentioned in the FacebookRedirectLoginHelper() method. Use the method getSessionFromRedirect() to check if response has been sent and if it is true get all the objects returned in the response. Here you can either store the objects in session or you may store it in database if you are planning to provide user login. For this you need to have a separate users table. In this tutorial for demonstration purpose I have used session to take data to index.php

That’s it. The template used in the preview is open source and available for use personally or officially. Do not forget to add your APPID, APPSECRET and Redirect URL.

Download   Preview

About Ashik

I am a Full Stack Developer and love to work on APIs and Apps. Hardcore lover of Ionic and Laravel <3

Check Also

Weather forecast API using jQuery, Ajax and PHP

Weather forecast API is widely used as widgets in small blogs to huge news website …

Do you wan't to get notified?

I will not spam your inbox. I will only send email whenever I publish a new article or to share information about technology

You have Successfully Subscribed!