Oauth Login for Linkedin, Facebook and Google

OAuth 2.0 is an open authorization protocol which enables applications to access each others data. For instance, a game application can access a users data in the Facebook application, or a location based application can access the user data of the Foursquare application etc.

Here is a diagram illustrating the concept:

Example of how OAuth 2.0 is used to share data via applications.
Example of how OAuth 2.0 is used to share data via applications.

The user accesses the game web application. The game web application asks the user to login to the game via Facebook. The user logs into Facebook, and is sent back to the game. The game can now access the users data in Facebook, and call functions in Facebook on behalf of the user (e.g. posting status updates).

 

OAuth 2.0 Use Cases

OAuth 2.0 can be used either to create an application that can read user data from another application (e.g. the game in the diagram above), or an application that enables other applications to access its user data (e.g. Facebook in the example above).

OAuth 2.0 is a replacement for OAuth 1.0, which was more complicated. OAuth 1.0 involved certificates etc. OAuth 2.0 is more simple. It requires no certificates at all, just SSL / TLS.

OAuth 2.0 Specification

The purpose of this tutorial is to provide an overview of the OAuth 2.0 protocol that is easy to understand. It is not the purpose to describe every detail of the specification, though.

If you plan to implement OAuth 2.0 you will most likely need to visit the specification to study it in full detail.

Use OAuth API with Facebook

1. Sign up for an Apigee Free account or Sign in.

2. Create an application on apigee.com

  1. Visit: https://apigee.com/oauth_api
  2. Click on the Add App button to launch the setup form.
  3. Name your OAuth API App.
  4. Confirm that the Base URL is what you want for your app.(You’ll make all API calls through this URL.).
  5. Click Create Base URL.

3. Add Facebook to your App

  1. At Apigee OAuth API app setup form…
  2. Select Facebook from the API Providers dropdown.
  3. Copy the Site URL, you’ll need this for Facebook
  4. View details of creating applications in Facebook (opens in new window).
  5. Visit https://developers.facebook.com/apps to launch a new window/tab with Facebook.
  6. Sign in to Facebook and create a new application.
  7. Fill out the form.
  8. Accept terms and create your Facebook App.
  9. Paste the Site URL in the integration section for “website”
  10. Copy your Facebook App App ID and paste it into the Apigee OAuth API setup form.
  11. Copy your Facebook App App Secret and paste it into the Apigee OAuth API setup form.
  12. Click Finish to complete your OAuth API App.
  13. When you return to the OAuth API apps list page, make a note of your Base URL, you’ll need this later.

4. Add a User for your App

Every user of your app gets a unique token, called a SmartKey. Every time you create a user, you get a SmartKey in return. Apigee will associate Facebook credentials with that SmartKey.

So now you’ll create your first user. The fastest way is to use the Apigee Console to make an API request to the OAuth API. This will help teach you how to use the OAuth API so that when you’re ready, you can build support for adding user into your app.

  1. In the Console
  2. Click on the Service dropdown and select the base URL: https://{appname}-api.apigee.com/v1
  3. Choose the expand method list icon and select the Create User method from the Managing Users group
  4. On the Template tab, supply your app name as the value
  5. One the Body tab and supply the values for userName, fullName, and password
  6. Send your request
  7. Expect a response that looks like the following:
    applicationUserId: 322, 
    fullName: "Demo User1", 
    smartKey: "4ec64ef1-7361-456e-95cf-562ed39fddd3", 
    userName: "demouser1", 
    appName:  "apigeedemoapp" 
    
  8. Make a note of the SmartKey value you get from the Console. You’ll use that next.

Authenticate the user with Facebook

  1. Open a new browser window
  2. Assemble the AuthURL for Facebook. Copy and paste this into your location bar:
    https://REPLACE_WITH_YOUR_APPNAME-api.apigee.com/v1/providers/facebook/authorize?smartkey=PASTE_SMARTKEY
    
  3. Paste your Auth URL to initiate the OAuth Dance with Facebook.
  4. Change the appname to your appname in the URI.
  5. Change the smartkey for a user that has been authorized with Facebook on the end of the URI.
  6. Hit return to submit the URL.(You’ll be redirected to Facebook).
  7. After signing in with Facebook and authorizing the app to access Facebook, you’re done.
  8. That’s it! All of the credential updating for your user is done behind the scenes in the Apigee Gateway.

5. Make authenticated requests with a Smartkey

  1. Open a new browser window
  2. Copy and paste this into your location bar:
    https://REPLACE_WITH_YOUR_APPNAME-api.apigee.com/v1/facebook/me?smartkey=PASTE_SMARTKEY
    
  3. Change the appname to your appname in the URI.
  4. Change the smartkey for a user that has been authorized with Facebook on the end of the URI.
  5. Hit return to submit the URL.
  6. You should see the JSON of a Facebook response, without any of the mess with OAUTH.

Facebook OAuth

base_facebook.php
Facebook.php

Google Plus OAuth

Googleplus.php

LinkedIn

OAuth.php
linkedin.php

 Usage

function verify_facebook($userdata) {

$users=$userdata[‘user’];

if(isset($userdata[‘user’]) && $userdata[‘user’][‘id’] && isset($users[’email’]) ) {

$email=$users[’email’];
$this->db->where(’email’, $email);
$this->db->where(‘user_type’, ‘USER’);
// Run the query
$query = $this->db->get(‘tta_users’);

// Let’s check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$this->db->where(‘user_id’,$row->user_id);

$data =
array(
‘user_id’ => $row->user_id,
‘first_name’ => $row->first_name,
‘last_name’ => $row->last_name,
’email’ => $row->email,
‘facebook’ => ‘no’,
‘logout’ => $userdata[‘url’]
);
$this->session->set_userdata($data);
return true;
} else {
//Registration process with facebook data
$this->db->set(‘first_name’,$users[‘first_name’] );
$this->db->set(‘last_name’, $users[‘last_name’]);
$this->db->set(’email’, $users[’email’]);
$this->db->set(‘password’, “AES_ENCRYPT(‘facebook2014’, ‘ttantra’)”, FALSE);
$this->db->set(‘user_type’, ‘USER’);
$this->db->set(‘status’, 1);
$this->db->insert(‘tta_users’);
$user_id=$this->db->insert_id();
$data =
array(
‘user_id’ => $user_id,
‘first_name’ => $users[‘first_name’],
‘last_name’ => $users[‘last_name’],
’email’ => $users[’email’],
‘facebook’ => ‘yes’,
‘logout’ => $userdata[‘url’]
);
$this->session->set_userdata($data);
return true;
}
}

}

function verify_linkedin($userdata) {

$users=$userdata[‘user’][‘linkedin’];

if(isset($userdata[‘user’][‘linkedin’]) && isset($users[’emailAddress’]) ) {

$email=$users[’emailAddress’];
$this->db->where(’email’, $email);
$this->db->where(‘user_type’, ‘USER’);
// Run the query
$query = $this->db->get(‘tta_users’);

// Let’s check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$this->db->where(‘user_id’,$row->user_id);

$data =
array(
‘user_id’ => $row->user_id,
‘first_name’ => $row->first_name,
‘last_name’ => $row->last_name,
’email’ => $row->email,
‘linkedin’ => ‘yes’
);
$this->session->set_userdata($data);
return true;
} else {
//Registration process with facebook data
$this->db->set(‘first_name’,$users[‘firstName’] );
$this->db->set(‘last_name’, $users[‘lastName’]);
$this->db->set(’email’, $users[’emailAddress’]);
$this->db->set(‘password’, “AES_ENCRYPT(‘linkedin2014’, ‘ttantra’)”, FALSE);
$this->db->set(‘user_type’, ‘USER’);
$this->db->set(‘status’, 1);
$this->db->insert(‘tta_users’);
$user_id=$this->db->insert_id();
$data =
array(
‘user_id’ => $user_id,
‘first_name’ => $users[‘firstName’],
‘last_name’ => $users[‘lastName’],
’email’ => $users[’emailAddress’],
‘linkedin’ => ‘yes’
);
$this->session->set_userdata($data);
return true;
}
}
}

function verify_google($userdata) {

$users=$userdata[‘user’];

if(isset($userdata[‘user’]) && $userdata[‘user’][‘id’] && isset($users[’emails’]) ) {

$email=$users[’emails’][0][‘value’];
$this->db->where(’email’, $email);
$this->db->where(‘user_type’, ‘USER’);
// Run the query
$query = $this->db->get(‘tta_users’);

// Let’s check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$this->db->where(‘user_id’,$row->user_id);

$data =
array(
‘user_id’ => $row->user_id,
‘first_name’ => $row->first_name,
‘last_name’ => $row->last_name,
’email’ => $row->email,
‘facebook’ => ‘no’
);
$this->session->set_userdata($data);
return true;
} else {
//Registration process with facebook data
$this->db->set(‘first_name’,$users[‘name’][‘givenName’] );
$this->db->set(‘last_name’, $users[‘name’][‘familyName’]);
$this->db->set(’email’, $users[’emails’][0][‘value’]);
$this->db->set(‘password’, “AES_ENCRYPT(‘google2014’, ‘ttantra’)”, FALSE);
$this->db->set(‘user_type’, ‘USER’);
$this->db->set(‘status’, 1);
$this->db->insert(‘tta_users’);
$user_id=$this->db->insert_id();
$data =
array(
‘user_id’ => $user_id,
‘first_name’ => $users[‘name’][‘givenName’],
‘last_name’ => $users[‘name’][‘familyName’],
’email’ => $users[’emails’][0][‘value’],
‘facebook’  => ‘no’,
‘google’    => ‘yes’,
‘google_url’    => $userdata[‘google_url’]
);
$this->session->set_userdata($data);
return true;
}
}

}

4 Comments

  1. Silagra

    Useful information. Lucky me I discovered your website accidentally, and I’m shocked why this accident didn’t took place in advance!

    I bookmarked it.

    Reply
  2. trading method

    It’s really a great and helpful piece of information. I am glad
    that you shared this helpful info with us. Please stay us up to date like this.

    Thank you for sharing.

    Reply
  3. avia tour travel

    I have read so many posts regarding the blogger lovers but this post is in fact a good post, keep it up.

    Reply
  4. togel hongkong

    Hello there, just became aware of your blog through Google, and found that it’s truly informative.

    I am going to watch out for brussels. I’ll appreciate if you
    continue this in future. A lot of people will be benefited from your writing.
    Cheers!

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *