Question

Im trying to login to google using php curl. I found this post:

Login to Google with PHP and Curl, Cookie turned off?

It worked great. But my objective is to stay logged in after posting the login request. I want to maintain the browser session so when i open gmail or any google service in new tab it should stayed login.

Is it possible? If yes, any help or suggestion will be highly appreciated.

Please let me know if my question is not clear enough.

Thanks!

No correct solution

OTHER TIPS

<?php

/* Google App Client Id */
define('CLIENT_ID', 'xxxxxxxxxxxxxxxxx');

/* Google App Client Secret */
define('CLIENT_SECRET', 'xxxxxxxxxxxxxxx');

/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'xxxxxxxxxxxxxxx');

$login_url = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online';
if(!$_GET['a'] == callback){
?>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
  <style type="text/css">
    #customBtn {
      display: inline-block;
      background: white;
      color: #444;
      width: 190px;
      border-radius: 5px;
      border: thin solid #888;
      box-shadow: 1px 1px 1px grey;
      white-space: nowrap;
    }
    #customBtn:hover {
      cursor: pointer;
    }
    span.label {
      font-family: serif;
      font-weight: normal;
    }
    span.icon {
      background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAB2ElEQVR4AWP4T2NAJwtGLRi14NeNq5+6m99lxb8OcH7lbfs2LuhTd9PP08epYMGfF8/fF2W8cjbFit7lp/x59oR8C35dvfQ62B1oEB70vjSbTAv+vH752t8J2ay38cEfmyo/NJS/iQ2E+iAj9u+nj2Ra8L40E270mwjvH8cOIcv+OLTvQ1UBxHRyLPj37vCXXvFXbsYg08M8/zx9QuVU9Oda+u89rN+X8r0J1v++cwv1k+nv44ZAC4Do5w6Jf3/+IEu1bfyBFR26/psUC/YLQyz4fdoeTcqp9QtWNGnHD9pa0LOFJAuOG0As+HxQ7s9flCCKm/4VjiKnfIVbMGf/TxIs+HklBWj61R3Cfqs8ttzbjysC15/+Bbdg1yVS4uD7673rtyhZrQgwWRHssSHl8ecXmGo+fP0XMw3qA+fWL0AuaRkte38T0HQI8t6YdujpaWTZG+/uJcy7B3d+28bvJOfkl1/fOK6NA5oOR0FbciqO9lYf64/ZWQrkmi6Ltu0/CDTdt+fLiw9/ySnsLr2+6bY+CWgWHuS9cPG5+3/IL66ff3mVvq8Ol+khW/Ouv7tLhQrn6tvbTSenxu0qd1oXb7060m9zZtWx/j2Pjv3993c41MmjFoxaAAB8n/tkbTPVXwAAAABJRU5ErkJggg==') transparent 5px 50% no-repeat;
      display: inline-block;
      vertical-align: middle;
      width: 42px;
      height: 42px;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: 42px;
      padding-right: 42px;
      font-size: 14px;
      font-weight: bold;
      /* Use the Roboto font that is loaded in the <head> */
      font-family: 'Roboto', sans-serif;
    }
  </style>
</head>

<body>
  <div id="gSignInWrapper">
    <a href="<?= $login_url ?>"><div id="customBtn" class="customGPlusSignIn">
      <span class="icon"></span>
      <span class="buttonText">Google</span>
    </div></a>
  </div>
</body>
<?php
} else {
// Holds the various APIs functions
function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) { 
    $url = 'https://www.googleapis.com/oauth2/v4/token';            

    $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_URL, $url);        
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
    curl_setopt($ch, CURLOPT_POST, 1);      
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);    
    $data = json_decode(curl_exec($ch), true);
    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      
    if($http_code != 200) 
        throw new Exception('Error : Failed to receieve access token');
    
    return $data;
}

function GetUserProfileInfo($access_token) {    
    $url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';  
    
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_URL, $url);        
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
    $data = json_decode(curl_exec($ch), true);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);     
    if($http_code != 200) 
        throw new Exception('Error : Failed to get user information');
        
    return $data;
}

// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
    try {
        // Get the access token 
        $data = GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);

        // Access Token
        $access_token = $data['access_token'];
        
        // Get user information
        $user_info = GetUserProfileInfo($access_token);
        
        var_dump($user_info);

        echo $user_info['id'] . "<br>";

        echo $user_info['email'] . "<br>";
        
        echo $user_info['name'] . "<br>";
        
        echo "<img src=".$user_info['picture'].">";
        


    }
    catch(Exception $e) {
        echo $e->getMessage();
        exit();
    }
}
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top