在我的网络应用程序中,我使用谷歌api php客户端访问谷歌任务。我已经学过了here等教程。
$client = new Google_Client();
$tasksService = new Google_TasksService($client);
$client->setAccessType('offline');
$cookie = $_COOKIE["token1"];
if(!empty($cookie)){
$client->refreshToken($cookie);
}
else{
$client->setAccessToken($client->authenticate($_GET['code']));
$_SESSION['access_token'] = $client->getAccessToken();
$sessionToken = json_decode($_SESSION['access_token']);
setcookie("token1", $sessionToken->refresh_token, time() + (20 * 365 * 24 * 60 * 60));
}
当用户点击登录URL时,他将进入权限屏幕。如果用户点击“允许访问”,他将作为经过身份验证的用户重定向到网页,然后将refresh_token存储在Cookie中。如果在cookie中存储refresh_token,则用户将被重定向而不再“允许访问”。我的代码中的问题发生在用户注销时,他们可以作为已注销的用户访问该站点。如何解决问题?
谢谢!
答案 0 :(得分:0)
不要将令牌存储在用户的cookie中(出于各种原因,包括您的直接问题,这是一个坏主意)。您已将其存储在会话中。只需从$ _SESSION中检索它。然后确保在用户注销时会话到期。
答案 1 :(得分:0)
更加用户友好的方法是将授权代码存储在与用户id相关联的数据库中。然后,当他登录时,可以获取它而无需用户再次授权(除非它已经过期 - 如果在尝试使用它时出现错误,则只需再次授权)。这些是我用来存储授权代码和用户ID的函数。这些_store和_get函数的变体可用于存储与用户关联的任何数据。
function _store_auth_code($auth_code) {
$entry = array('auth_code'=> $auth_code,
'uid' => $GLOBALS['user']->uid,
);
flashum_entry_delete('flashum_auth_code', $entry);
flashum_entry_insert('flashum_auth_code', $entry);
}
function _get_auth_code() {
$entry = array('uid' => $GLOBALS['user']->uid,
);
$return = flashum_entry_load('flashum_auth_code', $entry);
if ($return) {
return $return[0]->auth_code;
} else
return null;
}
function flashum_entry_load($db, $entry = array()) {
// Read all fields from the flashum table.
$select = db_select($db, 'flashum');
$select->fields('flashum');
// Add each field and value as a condition to this query.
foreach ($entry as $field => $value) {
$select->condition($field, $value);
}
// Return the result in object format.
return $select->execute()->fetchAll();
}
function flashum_entry_delete($db, $entry) {
db_delete($db)
->condition('uid', $entry['uid'])
->execute();
}
function flashum_entry_insert($db, $entry) {
$return_value = NULL;
try {
$return_value = db_insert($db)
->fields($entry)
->execute();
}
catch (Exception $e) {
drupal_set_message(t('db_insert failed. Message = %message, query= %query',
array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
}
return $return_value;
}