.

.

Twitter Login from outside in magento programattically

<?php
require_once 'app/Mage.php';

//twitter
require_once 'app/code/community/Inchoo/SocialConnect/controllers/TwitterController.php';
require_once 'app/code/community/Inchoo/SocialConnect/Helper/Twitter.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Twitter/Client.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Twitter/Userinfo.php';
Mage::app('default');  
Mage::getSingleton("core/session", array("name" => "frontend"));
?>
<?php
$data=array();

//get twitterid

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


// get first name & last name

$fullname= $_REQUEST['fullName'];
//print_r($fullname);
 $fname = $fullname[0];

if($fname=='')
{
    $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 token
  $token=$_REQUEST['token'];

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


//start for email by screen Name
$screen_name=$_REQUEST['screenName'];
  $email = sprintf('%s@twitter-user.com', strtolower($screen_name));

//end for email by screen Name

if($fname!='' && $lastname!=''  && $token!='' && $twitterid!='')
{
    // start here twitter code
  
    //$client = Mage::getSingleton('inchoo_socialconnect/twitter_client');
    //print_r($clientnew);

      //$token = $client->getAccessToken();
      
       /* $userInfo = (object) array_merge(
                (array) ($userInfo = $client->api('/account/verify_credentials.json', 'GET', array('skip_status' => true))),
                array('email' => sprintf('%s@twitter-user.com', strtolower($userInfo->screen_name)))
        );
*/
        $customersByTwitterId = Mage::helper('inchoo_socialconnect/twitter')->getCustomersByTwitterId($twitterid);
      
      

        if(Mage::getSingleton('customer/session')->isLoggedIn()) {          
      
            // Logged in user
            if($customersByTwitterId->count()) {
                // Twitter account already connected to other account - deny
                Mage::getSingleton('core/session')->addNotice('Your Twitter account is already connected to one of our store accounts.');
              
                    $data['responseCode']='1';
                  
                    $data['msg']='Your Twitter account is already connected to one of our store accounts';
                    $data['id']=Mage::getSingleton('core/session')->getCustomer()->getId();
echo json_encode($data);
                return;
            }
            // Connect from account dashboard - attach
            $customer = Mage::getSingleton('customer/session')->getCustomer();
           Mage::helper('inchoo_socialconnect/twitter')->connectByTwitterId($customer,$twitterid,$token);
/*         
 Mage::getSingleton('core/session')->addSuccess('Your Twitter account is now connected to your store accout. You can now login using our Twitter Connect button or using store account credentials you will receive to your email address.');*/
            $data['responseCode']='1';
            $data['msg']='Your Twitter account is now connected to your store accout. You can now login using our Twitter Connect button or using store account credentials you will receive to your email address';
            $data['id']=$customer->getId();          
echo json_encode($data);
            return;
        }

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

           $twittcustomer = Mage::helper('inchoo_socialconnect/twitter')->loginByCustomer($customer);

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

            return;
        }

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

        if($customersByEmail->count()) {
            // Email account already exists - attach, login
            $customer = $customersByEmail->getFirstItem();

           $twittcustomer= Mage::helper('inchoo_socialconnect/twitter')->connectByTwitterId($customer,$twitterid,$token);

            Mage::getSingleton('core/session')->addSuccess('We have discovered you already have an account at our store. Your Twitter 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 Twitter account is now connected to your store account';
            //$data['id']=Mage::getSingleton('core/session')->getCustomer->getId();
            echo json_encode($data);
            return;
        }

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

        Mage::helper('inchoo_socialconnect/twitter')->connectByCreatingAccount($email,$fname,$twitterid,$token);
        Mage::getSingleton('core/session')->addSuccess('Your Twitter account is now connected to your new user accout at our store.');             
        Mage::getSingleton('core/session')->addNotice(sprintf('Since Twitter doesn\'t support third-party access to your email address, we were unable to send you your store accout credentials. To be able to login using store account credentials you will need to update your email address and password using our <a href="%s">Edit Account Information</a>.'), Mage::getUrl('customer/account/edit'));       
        $data['responseCode']='0';
        $data['msg']='Your Twitter account is now connected to your new user accout at our store.Since Twitter doesn\'t support third-party access to your email address, we were unable to send you your store accout credentials. To be able to login using store account credentials you will need to update your email address and password';     
     echo json_encode($data);
          
            return;
  
    //end here twitter code
  
    }
  
    else
    {
        $data['responseCode']='0';
        $data['msg']='Please Enter Appropriate Fields';
        echo json_encode($data);
return;      
        }
  
  
  

 ?>


EmoticonEmoticon