【问题标题】:Refresh token google api php library刷新令牌 google api php 库
【发布时间】:2013-04-10 22:49:43
【问题描述】:

在我的网络应用程序中,我使用 google api php 客户端来访问 google 任务。我遵循了一些教程,例如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 中。如果 refresh_token 存储在 cookie 中,用户将被重定向而无需再次“允许访问”。我的代码中的问题发生在用户注销时,他们可以作为已注销的用户访问该站点。如何解决问题?

谢谢!

【问题讨论】:

    标签: api cookies refresh token


    【解决方案1】:

    不要将令牌存储在用户的 cookie 中(出于各种原因,包括您的直接问题,这是一个坏主意)。您已经将它存储在会话中。也只需从 $_SESSION 中检索它。然后确保会话在用户注销时过期。

    【讨论】:

      【解决方案2】:

      一种对用户更友好的方法是将授权代码存储在与用户 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;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-26
        • 2012-12-27
        • 2016-08-03
        • 2020-02-06
        • 2012-06-25
        • 2021-05-15
        • 2012-02-15
        • 2014-11-25
        • 2016-12-13
        相关资源
        最近更新 更多