Google OAuth 2.0:刷新 access_token 和新 refresh_token
Google OAuth 2.0: Refresh access_token and New refresh_token
问题
我很困惑,当用以前获得的 refresh_token
刷新 access_token
时,return 不是新的 refresh_token
。
Google OAuth 2.0 步骤
请求authentication_code
以下 URL,在默认浏览器中打开,正在请求身份验证代码,access_type=offline
到 return refresh_token
应用 returned 代码来检索access_token
:
https://accounts.google.com/o/oauth2/v2/auth?
access_type=offline
&prompt=consent
&response_type=code
&client_id=[** CLIENT_ID **]
&redirect_uri=http://localhost
&state=[** STATE **]
&scope=https%3a%2f%2fwww.googleapis.com%2fauth%2fuserinfo.email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fuserinfo.profile%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive
解析 redirect_uri
为 authentication_code
returnedredirect_uri=http://localhost
的查询字符串属性解析如下:
{
"state": "[** STATE **]",
"code": "[** AUTH CODE **]",
"scope": "email profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive openid",
"authuser": "1",
"hd": "[** HOST DOMAIN **]",
"prompt": "consent",
"session_state": "[** SESSION STATE **]"
}
请求 access_token
使用 authentication_code
使用 returned [** AUTH CODE **]
,请求 access_token
和 refresh_token
:
curl "https://www.googleapis.com/oauth2/v4/token" \
--request POST \
--silent \
--data 'grant_type=authorization_code
&code=[** AUTH CODE **]
&client_id=[** CLIENT_ID **]
&client_secret=[** CLIENT_SECRET **]
&redirect_uri=http://localhost
&state=[** STATE **]'
响应包括 access_token
和 refresh_token
:
{
"access_token": "[** ACCESS_TOKEN **]",
"expires_in": 3599,
"refresh_token": "[** REFRESH_TOKEN **]",
"scope": "openid https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
"token_type": "Bearer",
"id_token": "[** TOKEN ID **]"
}
刷新 access_token
使用 refresh_token
下次测试 refresh_token
刷新 access_token
:
curl "https://www.googleapis.com/oauth2/v4/token" \
--request POST \
--verbose \
--connect-timeout 60 \
--silent \
--data 'grant_type=refresh_token
&client_id=[** CLIENT_ID **]
&client_secret=[** CLIENT_SECRET **]
&refresh_token=[** REFRESH_TOKEN **]'
响应包括 access_token
但不包括 refresh_token
:
{
"access_token": "[** ACCESS_TOKEN **]",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive openid",
"token_type": "Bearer",
"id_token": "[** TOKEN ID **]"
}
保存refresh_token
?
不过,看来如果我重复使用最初提供的refresh_token=[** REFRESH_TOKEN **]
,无限刷新access_token
还是不错的。
因此,似乎 refresh_token=[** REFRESH_TOKEN **]
必须在初始同意后存储,这对于 OAuth 2.0 服务来说很奇怪。
上一个帖子
之前有一个 Stack Overflow 发布于 2015 年 3 月:
To get a new refresh token for your client you first need to revoke
the existing/old refresh token by revoking access for your client in
the Account Permissions tab for your Google account and then ask for
access_type=offline again.
在其他 OAuth 2.0 服务中,每次执行刷新请求时都会提供一个新的 refresh_token
,不需要去
总结
有没有其他方法可以每次用得到一个新的refresh_token
来得到一个新的access_token
?其他 OAuth 2.0 解决方案在处理刷新方面没有那么复杂。
谢谢,感谢您的回复。
由于最初提供的refresh_token
在刷新access_token
时无限期有效并且有效,那么好吧。
问题
我很困惑,当用以前获得的 refresh_token
刷新 access_token
时,return 不是新的 refresh_token
。
Google OAuth 2.0 步骤
请求authentication_code
以下 URL,在默认浏览器中打开,正在请求身份验证代码,access_type=offline
到 return refresh_token
应用 returned 代码来检索access_token
:
https://accounts.google.com/o/oauth2/v2/auth?
access_type=offline
&prompt=consent
&response_type=code
&client_id=[** CLIENT_ID **]
&redirect_uri=http://localhost
&state=[** STATE **]
&scope=https%3a%2f%2fwww.googleapis.com%2fauth%2fuserinfo.email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fuserinfo.profile%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive
解析 redirect_uri
为 authentication_code
returnedredirect_uri=http://localhost
的查询字符串属性解析如下:
{
"state": "[** STATE **]",
"code": "[** AUTH CODE **]",
"scope": "email profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive openid",
"authuser": "1",
"hd": "[** HOST DOMAIN **]",
"prompt": "consent",
"session_state": "[** SESSION STATE **]"
}
请求 access_token
使用 authentication_code
使用 returned [** AUTH CODE **]
,请求 access_token
和 refresh_token
:
curl "https://www.googleapis.com/oauth2/v4/token" \
--request POST \
--silent \
--data 'grant_type=authorization_code
&code=[** AUTH CODE **]
&client_id=[** CLIENT_ID **]
&client_secret=[** CLIENT_SECRET **]
&redirect_uri=http://localhost
&state=[** STATE **]'
响应包括 access_token
和 refresh_token
:
{
"access_token": "[** ACCESS_TOKEN **]",
"expires_in": 3599,
"refresh_token": "[** REFRESH_TOKEN **]",
"scope": "openid https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
"token_type": "Bearer",
"id_token": "[** TOKEN ID **]"
}
刷新 access_token
使用 refresh_token
下次测试 refresh_token
刷新 access_token
:
curl "https://www.googleapis.com/oauth2/v4/token" \
--request POST \
--verbose \
--connect-timeout 60 \
--silent \
--data 'grant_type=refresh_token
&client_id=[** CLIENT_ID **]
&client_secret=[** CLIENT_SECRET **]
&refresh_token=[** REFRESH_TOKEN **]'
响应包括 access_token
但不包括 refresh_token
:
{
"access_token": "[** ACCESS_TOKEN **]",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive openid",
"token_type": "Bearer",
"id_token": "[** TOKEN ID **]"
}
保存refresh_token
?
不过,看来如果我重复使用最初提供的refresh_token=[** REFRESH_TOKEN **]
,无限刷新access_token
还是不错的。
因此,似乎 refresh_token=[** REFRESH_TOKEN **]
必须在初始同意后存储,这对于 OAuth 2.0 服务来说很奇怪。
上一个帖子
之前有一个 Stack Overflow 发布于 2015 年 3 月:
To get a new refresh token for your client you first need to revoke the existing/old refresh token by revoking access for your client in the Account Permissions tab for your Google account and then ask for access_type=offline again.
在其他 OAuth 2.0 服务中,每次执行刷新请求时都会提供一个新的 refresh_token
,不需要去
总结
有没有其他方法可以每次用得到一个新的refresh_token
来得到一个新的access_token
?其他 OAuth 2.0 解决方案在处理刷新方面没有那么复杂。
谢谢,感谢您的回复。
由于最初提供的refresh_token
在刷新access_token
时无限期有效并且有效,那么好吧。