.

.

Google Login from Outside in magento Programattically


I used Inchoo Social Login Extension in Magento For My web Application. And also I need social login webservices For my magento android app. Here I have done webservices for Google login By using Inchoo Social Extension.

<?php
require_once 'app/Mage.php';
require_once 'app/code/community/Inchoo/SocialConnect/controllers/GoogleController.php';
require_once 'app/code/community/Inchoo/SocialConnect/Helper/Google.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Google/Client.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Google/Userinfo.php';

//facebook
require_once 'app/code/community/Inchoo/SocialConnect/controllers/FacebookController.php';
require_once 'app/code/community/Inchoo/SocialConnect/Helper/Facebook.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Facebook/Client.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Facebook/Userinfo.php';

Mage::app('default');   
Mage::getSingleton("core/session", array("name" => "frontend"));
$data=array();

$googleid = $_REQUEST['googleid'];
if($googleid=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter Google Id';
    echo json_encode($data);
return;
}

// get first name & last name

 $gname=$_REQUEST['fullname'];
//print_r($gname);
 $fullname= explode(' ',$gname);

 $firstname = $fullname[0];
if($firstname=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter FirstName';
    echo json_encode($data);
return;
    }
   
 $lastname = $fullname[1];
if($lastname=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter LastName';
    echo json_encode($data);
return;
    }

//get Email Id
 $email=$_REQUEST['email'];
if($email=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter Email';
    echo json_encode($data);
return;
    }

// get token

 $token1=str_replace('.','',$_REQUEST['token']);
$token2=str_replace("'","",$token1);
$token=str_replace(",","",$token2);


if($token=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter Token';
    echo json_encode($data);
return;
}

if($firstname!='' && $lastname!='' && $email!='' && $token!='' && $googleid!='')
{
    // start here google code
   
     // Google API green light - proceed
            $client = Mage::getSingleton('inchoo_socialconnect/google_client');

            //$userInfo = $client->api('/userinfo');
            //$token = $client->getAccessToken();
            $token=$token;

            $customersByGoogleId = Mage::helper('inchoo_socialconnect/google')->getCustomersByGoogleId($googleid);

            if(Mage::getSingleton('customer/session')->isLoggedIn()) {
                // Logged in user
                if($customersByGoogleId->count()) {
                    // Google account already connected to other account - deny
                    Mage::getSingleton('core/session')->addNotice('Your Google account is already connected to one of our store accounts.');
                    $data['responseCode']='1';
                    $data['msg']='Your Google account is already connected to one of our store accounts';
echo json_encode($data);
return;
          
                }

                // Connect from account dashboard - attach
                $customer = Mage::getSingleton('customer/session')->getCustomer();

                Mage::helper('inchoo_socialconnect/google')->connectByGoogleId($customer,
                    $googleid,
                    $token
                );

                Mage::getSingleton('core/session')->addSuccess('Your Google account is now connected to your store accout. You can now login using our Google Connect button or using store account credentials you will receive to your email address.');
               
$data['responseCode']='1';
$data['msg']='Your Google account is now connected to your store accout. You can now login using our Google Connect button or using store account credentials you will receive to your email address';

echo json_encode($data);
return;
             
            }

            if($customersByGoogleId->count()) {
               
                // Existing connected user - login
                $customer = $customersByGoogleId->getFirstItem();

                Mage::helper('inchoo_socialconnect/google')->loginByCustomer($customer);

                Mage::getSingleton('core/session')->addSuccess('You have successfully logged in using your Google account.');
                $data['responseCode']='1';
                $data['msg']='You have successfully logged in using your Google account';
echo json_encode($data);
return;               
            }

            $customersByEmail = Mage::helper('inchoo_socialconnect/facebook')
                ->getCustomersByEmail($email);

            if($customersByEmail->count())  {
                // Email account already exists - attach, login
                $customer = $customersByEmail->getFirstItem();
               
                Mage::helper('inchoo_socialconnect/google')->connectByGoogleId(
                    $customer,
                    $googleid,
                    $token
                );

                Mage::getSingleton('core/session')->addSuccess('We have discovered you already have an account at our store. Your Google account is now connected to your store account.');
                $data['responseCode']='1';
                $data['msg']='We have discovered you already have an account at our store. Your Google account is now connected to your store account';
echo json_encode($data);
return;              
            }
                       

            // New connection - create, attach, login
            if(empty($firstname)) {
                throw new Exception('Sorry, could not retrieve your Google first name. Please try again.'
                );
                $data['responseCode']='0';
                $data['msg']='Sorry, could not retrieve your Google first name. Please try again';
                echo json_encode($data);
return;
            }

            if(empty($lastname)) {
                throw new Exception('Sorry, could not retrieve your Google last name. Please try again.'
                );
               
                $data['responseCode']='0';
                $data['msg']='Sorry, could not retrieve your Google last name. Please try again';
                echo json_encode($data);
return;
            }

            Mage::helper('inchoo_socialconnect/google')->connectByCreatingAccount($email,$firstname,$lastname,$googleid,            $token );

            Mage::getSingleton('core/session')->addSuccess('Your Google account is now connected to your new user accout at our store. Now you can login using our Google Connect button or using store account credentials you will receive to your email address.');
   
    $data['responseCode']='1';
    $data['msg']='Your Google account is now connected to your new user accout at our store. Now you can login using our Google Connect button or using store account credentials you will receive to your email address';
   
    echo json_encode($data);
return;
       
    //end here google code   
    }
   
    else
    {
        $data['responseCode']='0';
        $data['msg']='Please Enter Appropriate Fields';
        echo json_encode($data);
return;       
        }

 ?>