1

I have an app set up and working for users to insert events in their calendars. I need to verify if they logged in with the correct google account. (work email). I have searched the web for hours with no luck. I've tried several different things with no luck :/ I hope someone can guide me or help me get this to work. Thank you! Here is what I have

$client = new Google_Client();
$client->setApplicationName("Intakes Calendar");
$client->setClientId('xxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxx');
$client->setRedirectUri('xxx');
$client->setDeveloperKey('xxx');
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
$client->addScope('https://www.googleapis.com/auth/userinfo.email');

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) {
    //echo "<br>Getting access";
    $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()){

    if($client->isAccessTokenExpired()) {

        $authUrl = $client->createAuthUrl();
        $button =  '<a class="btn btn-success btn-sm" href="'.$authUrl.'">SIGN IN</a>';
        $msg = '<DIV class="col-md-6 col-md-offset-3 alert alert-warning">You are not signed in with your work email account for remote sessions.' . $button . '</DIV>';

    }else{

        //if logged in check for user email 
        if($_SESSION['USER_EMAIL'] == 'GOOGLE LOGGED IN EMAIL?')
            $msg = ' <DIV class="col-md-6 col-md-offset-3 alert alert-warning">You are connected and ready for remote sessions.</DIV>';
        else
            $msg = ' <DIV class="col-md-6 col-md-offset-3 alert alert-warning">Please log in with your work email.</DIV>';
    }

}else{

    $authUrl = $client->createAuthUrl();
    $button =  '<a class="btn btn-success btn-sm" href="'.$authUrl.'">SIGN IN</a>';
    $msg = '<DIV class="col-md-6 col-md-offset-3 alert alert-warning">You are not signed in with your work email account for remote sessions.' . $button . '</DIV>';

}
joanb
  • 169
  • 1
  • 1
  • 17
  • What is your main issue here? You cant manage to get the current users email address? Could you try using ```$_SESSION["email"]``` instead of ```$_SESSION["USER EMAIL"]``` as stated in [this Stack answer](https://stackoverflow.com/a/22733931/12835757) ? Also, if that is the problem have you tried checking the Gmail API method ```getProfile()``` mentioned in [this documenation site](https://developers.google.com/gmail/api/v1/reference/users/getProfile)? – Mateo Randwolf Mar 23 '20 at 11:06
  • The issue here is that I need to verify which email account did the user used to sign in. I How can I use the getProfile? $client->getProfile()? – joanb Mar 23 '20 at 20:02

1 Answers1

0

Finally figured it out!!!

first you have to add the scope for gmail

$client->addScope('https://www.googleapis.com/auth/gmail.readonly');

then start gmail service and the user profile email address

$gmailService = new Google_Service_Gmail($client);
$users = $gmailService->users->getProfile('me');
echo $users['emailAddress']; //this is the logged in user's email address
joanb
  • 169
  • 1
  • 1
  • 17