Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm trying gmail login on my website. i used Oauth2 for this.
http://www.idiotminds.com/sign-in-with-google-account-using-oauth2-0/

Here process is working fine but i don't getting any data from the service like username , email etc. no any data is returning for me

http://22yardscricket.com/gmaillogin/index.php


Here is my code:
config.php:

VB
<?php
/*
------------------------------------------------------
  www.idiotminds.com
--------------------------------------------------------
*/
session_start();

$base_url= filter_var('http://22yardscricket.com/', FILTER_SANITIZE_URL);

// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
define('CLIENT_ID','883082907275-qgecuunt11au1pj49l5o0qn4prjks8oh.apps.googleusercontent.com');
define('CLIENT_SECRET','4JYX7-AFq5xui7GIpYhS56ck');
define('REDIRECT_URI','http://22yardscricket.com/gmaillogin/return.php');
define('APPROVAL_PROMPT','auto');
define('ACCESS_TYPE','online');
?>


index.php:
XML
<?php require_once("../classes/lib.php");?>
<?php
/*
------------------------------------------------------
  www.idiotminds.com
--------------------------------------------------------
*/
require_once 'config.php';
require_once 'lib/Google_Client.php';
require_once 'lib/Google_Oauth2Service.php';

$client = new Google_Client();
$client->setApplicationName("Google UserInfo PHP Starter Application");

$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setApprovalPrompt(APPROVAL_PROMPT);
$client->setAccessType(ACCESS_TYPE);

$oauth2 = new Google_Oauth2Service($client);

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  echo '<script type="text/javascript">window.close();</script>'; exit;
}

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

if (isset($_REQUEST['error'])) {
 echo '<script type="text/javascript">window.close();</script>'; exit;
}

if ($client->getAccessToken()) {
  $user = $oauth2->userinfo->get();

    $sql="INSERT INTO users (name,email,gender) VALUES ('".$user['name']."','".$user['email']."','".$user['gender']."')";
    mysql_query($sql);

  // These fields are currently filtered through the PHP sanitize filters.
  // See http://www.php.net/manual/en/filter.filters.sanitize.php
  $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
  $img = filter_var($user['picture'], FILTER_VALIDATE_URL);
  $personMarkup = "$email<div><img src='$img?sz=50'></div>";

$_SESSION["email"] = $email;
$_SESSION["name"] = $user['name'];

echo "<script>alert('".$email."');</script>";

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();

} else {
  $authUrl = $client->createAuthUrl();
}
?>
<!doctype html>
<html>
<head><meta charset="utf-8">
    <title>Signin with Google Account-Idiot Minds</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/oauthpopup.js"></script>
<script type="text/javascript">
$(document).ready(function(){

     $('a.login').oauthpopup({
            path: '<?php if(isset($authUrl)){echo $authUrl;}else{ echo '';}?>',
            width:650,
            height:350,
        });
        $('a.logout').googlelogout({
            redirect_url:'<?php echo $base_url; ?>logout.php'
        });

});
</script>

</head>
<body>
 <div style="float:left;width:33%;margin-left:375px;">
<?php if(isset($personMarkup)): ?>
<?php print $personMarkup ?>
<?php endif ?>
<?php
  if(isset($authUrl)) {
    print "<a class='login' href='javascript:void(0);'><img alt='Signin in with Google' src='signin_google.png'/></a>";
  } else {
   print "<a class='logout' href='javascript:void(0);'>Logout</a>";
  }
?>
</div>

</body></html>


kindly suggest.


Regards,
Harish
Posted
Updated 7-Dec-14 17:48pm
v2
Comments
ZurdoDev 7-Dec-14 22:13pm    
Please click Improve question and show relevant code and any errors you get.
harry madaan 7-Dec-14 23:49pm    
Hello Sir,

Question has been updated, please check now.

Regards,
Harish
harry madaan 9-Dec-14 0:08am    
Hi,

Have you checked my query?

Regards,
Harish

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900