0

I am able to login with Google by PHP. But could not find how to get the login email after login with which the user logged in. It is very important to get the login email. Please help me out. Thanks in advance. I am getting the code google returns after login and the access token. Here is my code:

  $client_id = '870872749542-hf3ll1kbpnucn8rm67p2s83h5j890og5.apps.googleusercontent.com';
  $client_secret = '_Q4sSVLD90RKZ5H1seRPrw6D';
  $redirect_uri = 'http://symmetrix.website/staging/property/gcontacts.php';
  $simple_api_key = 'AIzaSyANQX5L0uNzRb2wYOq3t0VHvVLcfoi71Gs';
  $max_results = 30;
  $auth_code = $_GET["code"];

  $fields = array(
    'code' => urlencode($auth_code),
    'client_id' => urlencode($client_id),
    'client_secret' => urlencode($client_secret),
    'redirect_uri' => urlencode($redirect_uri),
    'grant_type' => urlencode('authorization_code')
  );
  $post = '';
  foreach ($fields as $key => $value) {
    $post .= $key . '=' . $value . '&';
  }
  $post = rtrim($post, '&');

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
  curl_setopt($curl, CURLOPT_POST, 5);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  $result = curl_exec($curl);

  curl_close($curl);

  $response = json_decode($result);
  if (isset($response->access_token)) {
     $accesstoken = $response->access_token;
  }
Ayan
  • 165
  • 1
  • 1
  • 9
  • does [this](https://stackoverflow.com/questions/24442668/google-oauth-api-to-get-users-email-address) answer your question? – Aven Desta May 29 '21 at 08:07
  • Thanks for replying. But will I get the login email with https://openidconnect.googleapis.com/v1/userinfo? I.e. the email with which the user logged in? – Ayan May 29 '21 at 11:19
  • What is mean by "login email" here? Do you mean the "email address" used to logon? Or a "email message" send as a reaction to the logon event? – arkascha May 29 '21 at 12:12
  • Yes yes. The "email address" with which the user logged in. Please it is very important for me to get that. – Ayan May 29 '21 at 15:13

0 Answers0