15 Ocak 2013 Salı

Login with Google Account OAuth

Database
Sample database design 
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(50) UNIQUE
fullname VARCHAR(100),
firstname VARCHAR(50),
lastname VARCHAR(50),
google_id VARCHAR(50),
gender VARCHAR(10),
dob VARCHAR(15),
profile_image TEXT,
gpluslink TEXT
)

Step 1: Domain Registration
Add or register your domain at click here.

Importing GMail Contacts Google OAuth Connect with PHP.

Step 2: Ownership verification
Verify your domain ownership with HTML file upload or including META tag.

Importing GMail Contacts Google OAuth Connect with PHP.

Step 3: OAuth Keys
Google will provide you OAuth consumer key and OAuth consumer secret key.
Importing GMail Contacts Google OAuth Connect with PHP.

Step 4: Google APIConsole
Create client ID OAuth Console here. 
Login with Google Plus Oauth

Step 5:
Create client ID.
Login with Google Plus OAuth.

Step 6
Here the application OAuth client ID and client secret.
Login with Google Plus OAuth.

config.php
You can find this in src folder, here you have to configure application OAuth keys, Consumer keys and redirection callback URL.
// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console Step 6 keys 
'oauth2_client_id' => 'App Client ID',
'oauth2_client_secret' => 'App Client Secret',
'oauth2_redirect_uri' => 'http://yoursite.com/gplus/index.php',

// OAuth1 Settings Step 3  keys.
'oauth_consumer_key' => 'OAuth Consumer Key',
'oauth_consumer_secret' => 'OAuth Consumer Secret',

google_login.php
Google plus login system. Just include the file in index.php
<?php
require_once 'src/apiClient.php';
require_once 'src/contrib/apiOauth2Service.php';
session_start();
$client = new apiClient();
setApplicationName("Google Account Login");
$oauth2 = new apiOauth2Service($client);
if (isset($_GET['code'])) 
{
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
unset($_SESSION['google_data']); //Google session data unset
$client->revokeToken();
}

if ($client->getAccessToken()) 
{
$user = $oauth2->userinfo->get();
$_SESSION['google_data']=$user; // Storing Google User Data in Session
header("location: home.php");
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}

if(isset($personMarkup)): 
print $personMarkup;
endif

if(isset($authUrl)) 
{
echo "<a class="login" href="$authUrl">Google Account Login</a>";
} else {
echo "<a class="logout" href="?logout">Logout</a>";
}
?>

home.php
Contains PHP code inserting Google plus session details into users table. 
<?php
session_start();
include('db.php'); //Database Connection. 
if (!isset($_SESSION['google_data'])) {
// Redirection to application home page. 
header("location: index.php");
}
else
{
//echo print_r($userdata);
$userdata=$_SESSION['google_data'];
$email =$userdata['email'];
$googleid =$userdata['id'];
$fullName =$userdata['name'];
$firstName=$userdata['given_name'];
$lastName=$userdata['family_name'];
$gplusURL=$userdata['link'];
$avatar=$userdata['picture'];
$gender=$userdata['gender'];
$dob=$userdata['birthday'];
//Execture query
$sql=mysql_query("insert intousers(email,fullname,firstname,lastname,google_id,gender,dob,profile_image,gpluslink) values('$email','$fullName','$firstName','$lastName','$googleid','$gender','$dob','$avatar','$gplusURL')");
?>

db.php
Database Configuration file.
<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "databasename";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

Hiç yorum yok:

Yorum Gönder