Home > Backend Development > PHP Tutorial > Google Oauth2 获取用户信息,遇到一个诡异的问题

Google Oauth2 获取用户信息,遇到一个诡异的问题

WBOY
Release: 2016-06-06 20:18:12
Original
1746 people have browsed it

异常描述
我使用帐号 A 登录授权以后,获取到帐号 A 的信息,然后退出。
再使用另一个帐号 B 登陆授权以后,获取的信息依然是帐号 A 的信息。
但过上一段时间(无法确定多久时间),再使用帐号 B 登陆授权,可以获取帐号 B 的信息,再退出。
再使用帐号 A 登陆,获取的信息依然是帐号 B 的信息,这是怎么一回事情啊?

测试地址: http://www.euask.com/oauth/google.php

<code><?php require_once __DIR__ .'/g/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://www.euask.com/oauth/google.php');
$client->setScopes(array(
  'https://www.googleapis.com/auth/userinfo.email',
  'https://www.googleapis.com/auth/plus.login',
  'https://www.googleapis.com/auth/plus.me'
));
// $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$plus = new Google_Service_Oauth2($client);

if (isset($_REQUEST['logout'])) {
  session_unset();
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://www.euask.com/oauth/google.php';
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $me = $plus->userinfo->get();
  $id = $me['id'];
  $name = $me['givenName'];
  $email = $me['email'];
  $profile_image_url = $me['picture'];
} else {
  $auth_url = $client->createAuthUrl();
}

?>

<div>
  <?php if (isset($auth_url)) {
      echo "<a href='".filter_var($auth_url, FILTER_SANITIZE_URL)."'>Google login";
    } else {
      // print "ID: {$id} <br>";
      print "Name: {$name} <br>";
      print "Email: {$email} <br>";
      print "Image: {$profile_image_url} <br>";
      echo "<a href="?logout">Sign out</a>";
    }
  ?>
</div></code>
Copy after login
Copy after login

回复内容:

异常描述
我使用帐号 A 登录授权以后,获取到帐号 A 的信息,然后退出。
再使用另一个帐号 B 登陆授权以后,获取的信息依然是帐号 A 的信息。
但过上一段时间(无法确定多久时间),再使用帐号 B 登陆授权,可以获取帐号 B 的信息,再退出。
再使用帐号 A 登陆,获取的信息依然是帐号 B 的信息,这是怎么一回事情啊?

测试地址: http://www.euask.com/oauth/google.php

<code><?php require_once __DIR__ .'/g/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://www.euask.com/oauth/google.php');
$client->setScopes(array(
  'https://www.googleapis.com/auth/userinfo.email',
  'https://www.googleapis.com/auth/plus.login',
  'https://www.googleapis.com/auth/plus.me'
));
// $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$plus = new Google_Service_Oauth2($client);

if (isset($_REQUEST['logout'])) {
  session_unset();
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://www.euask.com/oauth/google.php';
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $me = $plus->userinfo->get();
  $id = $me['id'];
  $name = $me['givenName'];
  $email = $me['email'];
  $profile_image_url = $me['picture'];
} else {
  $auth_url = $client->createAuthUrl();
}

?>

<div>
  <?php if (isset($auth_url)) {
      echo "<a href='".filter_var($auth_url, FILTER_SANITIZE_URL)."'>Google login";
    } else {
      // print "ID: {$id} <br>";
      print "Name: {$name} <br>";
      print "Email: {$email} <br>";
      print "Image: {$profile_image_url} <br>";
      echo "<a href="?logout">Sign out</a>";
    }
  ?>
</div></code>
Copy after login
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template