Google 登录扩展令牌
Google sign in extended token
我的问题:
我怎样才能收到扩展令牌,这样用户就不会在一个小时左右后注销?我故意请求离线访问,因为我读到我必须这样做才能接收刷新令牌。但我没有在 authResult
java-脚本变量中收到它。
我的代码:
这是我的用户使用他们的 Google 帐户登录我的网站的方式:
1 - 显示登录按钮。
<span id="signinButton">
<span class="g-signin" data-callback="signinCallback" data-clientid="********.apps.googleusercontent.com" data-access_type="offline" data-cookiepolicy="single_host_origin" data-requestvisibleactions="http://schema.org/AddAction" data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/offline"></span>
</span>
2 - 用户单击按钮后,将进行 ajax 调用并将访问令牌传递到服务器。
function signinCallback(authResult) {
if (authResult['status']['signed_in']) {
//Shoot the ajax
$.ajax({
//...Send the access token to the server
})
.done(function(msg) {
//...Do somthing after a sucessfull login
});
} else {
//...
}
}
3 - PHP 函数接收令牌并将其保存在 $_SESSION
变量中供以后使用。
public function GoogleAuth($token) {
$_SESSION['google_token'] = $token;
#See that the token is valid
$session_status = $this->GetSessionStatus($token, 'all');
if ($session_status == false) {
return $session_status;
}
#If token is valid, manage the registration/login process
//...
}
4 - 这是检查令牌是否有效的函数:
public function GetSessionStatus($token) {
$url = 'https://www.googleapis.com/oauth2/v1/userinfo';
$params = 'access_token='.$token;
$ch = curl_init($url . '?' . $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$user = curl_exec($ch);
$user = json_decode($user);
curl_close($ch);
if (isset($user->id)) {
return true;
} else {
return false;
}
}
再问我的问题以防你忘记了:)
这就是我处理登录的方式。我不确定我做的是否正确,但它确实有效。
我的问题是我怎样才能收到扩展令牌,这样用户就不会在一个小时左右后注销。我故意请求离线访问,因为我读到我必须这样做才能接收刷新令牌。但我没有在 authResult
java-脚本变量中收到它。
编辑:最后(如何解决):
首先,我没有使用 $_SESSION
,而是使用了 $_COOKIE
。它更持久。
其次,我切换到所有 PHP sdk 以接收刷新令牌。
使用 Javascript 意味着您正在使用所谓的 OAuth 2.0 隐式授权,并且由于安全限制,该授权类型无法 return 刷新令牌。规范在 https://www.rfc-editor.org/rfc/rfc6749#section-4.2.2 部分说:
The authorization server MUST NOT issue a refresh token.
这是因为 Javascript 客户端无法安全地存储长期存在的令牌。您最好使用服务器端流程,例如使用 google-api-php-client
,这样您还可以以安全的方式存储和重复使用刷新令牌。
我的问题:
我怎样才能收到扩展令牌,这样用户就不会在一个小时左右后注销?我故意请求离线访问,因为我读到我必须这样做才能接收刷新令牌。但我没有在 authResult
java-脚本变量中收到它。
我的代码:
这是我的用户使用他们的 Google 帐户登录我的网站的方式:
1 - 显示登录按钮。
<span id="signinButton">
<span class="g-signin" data-callback="signinCallback" data-clientid="********.apps.googleusercontent.com" data-access_type="offline" data-cookiepolicy="single_host_origin" data-requestvisibleactions="http://schema.org/AddAction" data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/offline"></span>
</span>
2 - 用户单击按钮后,将进行 ajax 调用并将访问令牌传递到服务器。
function signinCallback(authResult) {
if (authResult['status']['signed_in']) {
//Shoot the ajax
$.ajax({
//...Send the access token to the server
})
.done(function(msg) {
//...Do somthing after a sucessfull login
});
} else {
//...
}
}
3 - PHP 函数接收令牌并将其保存在 $_SESSION
变量中供以后使用。
public function GoogleAuth($token) {
$_SESSION['google_token'] = $token;
#See that the token is valid
$session_status = $this->GetSessionStatus($token, 'all');
if ($session_status == false) {
return $session_status;
}
#If token is valid, manage the registration/login process
//...
}
4 - 这是检查令牌是否有效的函数:
public function GetSessionStatus($token) {
$url = 'https://www.googleapis.com/oauth2/v1/userinfo';
$params = 'access_token='.$token;
$ch = curl_init($url . '?' . $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$user = curl_exec($ch);
$user = json_decode($user);
curl_close($ch);
if (isset($user->id)) {
return true;
} else {
return false;
}
}
再问我的问题以防你忘记了:)
这就是我处理登录的方式。我不确定我做的是否正确,但它确实有效。
我的问题是我怎样才能收到扩展令牌,这样用户就不会在一个小时左右后注销。我故意请求离线访问,因为我读到我必须这样做才能接收刷新令牌。但我没有在 authResult
java-脚本变量中收到它。
编辑:最后(如何解决):
首先,我没有使用 $_SESSION
,而是使用了 $_COOKIE
。它更持久。
其次,我切换到所有 PHP sdk 以接收刷新令牌。
使用 Javascript 意味着您正在使用所谓的 OAuth 2.0 隐式授权,并且由于安全限制,该授权类型无法 return 刷新令牌。规范在 https://www.rfc-editor.org/rfc/rfc6749#section-4.2.2 部分说:
The authorization server MUST NOT issue a refresh token.
这是因为 Javascript 客户端无法安全地存储长期存在的令牌。您最好使用服务器端流程,例如使用 google-api-php-client
,这样您还可以以安全的方式存储和重复使用刷新令牌。