Google Oauth2 在身份验证时不返回刷新令牌
Google Oauth2 not returning Refresh token upon authentcation
我曾经为我的 Web 应用程序开发一个身份验证模块,该模块是使用 Oauth2 Google+ API
使用 Angular2
开发的,这里的问题是 google 服务器响应不包含任何刷新除了 access_token
、 expires_in
、 token_type
、 id_token
键之外的令牌,我试图撤销授予我的应用程序的访问权限并重试,但仍然是同样的问题;这是 POST
查询中添加的参数:
code=4/BXlTY3dLzpUMfu4WuapGjn1du6TCVXEt6Wxjf7-EK_E
&client_id=429410750270503bndl.apps.googleusercontent.com
&client_secret=*****GZe11WEgy9Glg2T
&grant_type=authorization_code
&redirect_uri=http://localhost:3000/loginplus
&access_type=offline
问题是在官方文档中 Google 说它应该 return 一个 refresh_token
但是我没有看到它,我只有上面提到的那些,这里是响应
{
access_token: "ya29..tgLONwAAje6nNWmqV7bMJ_xBQQVS5ZUpXK7m5rCiJwIeeQZTw70fYjOGoX3Z9ZBMvA",
token_type: "Bearer",
expires_in: 3597,
id_token: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjA3YjlhZDg5ZWFhMTQxNW…bMdi_TtH998qaSdpL05EYG3_1bRijX6AFQwhUoe9ILJeOZM5w"
}
我搜索了很多,但 none 的答案对我有帮助,有帮助吗??
使用 access_type=offline
的问题是您的应用程序很可能已经与用户交换了授权码。如果是这种情况,它会在第一次调用时发回 response_code
。您应该存储此代码以供进一步使用。
如果您想获得新的refresh_token
,您必须再次征求用户对使用您的应用程序的同意。
如需进一步参考,请阅读 this
特别是这个重要的部分:
Important: When your application receives a refresh token, it is important to store that refresh token for future use. If your application loses the refresh token, it will have to re-prompt the user for consent before obtaining another refresh token. If you need to re-prompt the user for consent, include the prompt parameter in the authorization code request, and set the value to consent.
tl;博士
approval_prompt=在这种情况下需要强制才能获得刷新令牌。
详情:
access_type=如果想要获取刷新令牌,请求中始终需要离线。 (PierreDuc 的另一个答案不正确)
但有时您在交换您获得的 "authorization code" 后可能无法获得刷新令牌。
为什么?
为了 return 一个新的刷新令牌,我们希望用户同意。如果用户已经对您的应用程序授予离线同意,我们知道该应用程序已经有一个刷新令牌(至少一个)并且通常不应该一次又一次地请求它。所以我们不显示批准页面和 return 仅提供访问令牌的代码。
如果之前的刷新令牌不知何故丢失了,而您想再次获取它,那么在请求中添加 approval_prompt=force 将向用户显示一个批准页面,并且还会为您提供一个代码,该代码将提供刷新令牌。
这已经更新,必要的查询参数现在是 prompt=consent
见https://developers.google.com/identity/protocols/OAuth2WebServer#redirecting
我曾经为我的 Web 应用程序开发一个身份验证模块,该模块是使用 Oauth2 Google+ API
使用 Angular2
开发的,这里的问题是 google 服务器响应不包含任何刷新除了 access_token
、 expires_in
、 token_type
、 id_token
键之外的令牌,我试图撤销授予我的应用程序的访问权限并重试,但仍然是同样的问题;这是 POST
查询中添加的参数:
code=4/BXlTY3dLzpUMfu4WuapGjn1du6TCVXEt6Wxjf7-EK_E
&client_id=429410750270503bndl.apps.googleusercontent.com
&client_secret=*****GZe11WEgy9Glg2T
&grant_type=authorization_code
&redirect_uri=http://localhost:3000/loginplus
&access_type=offline
问题是在官方文档中 Google 说它应该 return 一个 refresh_token
但是我没有看到它,我只有上面提到的那些,这里是响应
{
access_token: "ya29..tgLONwAAje6nNWmqV7bMJ_xBQQVS5ZUpXK7m5rCiJwIeeQZTw70fYjOGoX3Z9ZBMvA",
token_type: "Bearer",
expires_in: 3597,
id_token: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjA3YjlhZDg5ZWFhMTQxNW…bMdi_TtH998qaSdpL05EYG3_1bRijX6AFQwhUoe9ILJeOZM5w"
}
我搜索了很多,但 none 的答案对我有帮助,有帮助吗??
使用 access_type=offline
的问题是您的应用程序很可能已经与用户交换了授权码。如果是这种情况,它会在第一次调用时发回 response_code
。您应该存储此代码以供进一步使用。
如果您想获得新的refresh_token
,您必须再次征求用户对使用您的应用程序的同意。
如需进一步参考,请阅读 this
特别是这个重要的部分:
Important: When your application receives a refresh token, it is important to store that refresh token for future use. If your application loses the refresh token, it will have to re-prompt the user for consent before obtaining another refresh token. If you need to re-prompt the user for consent, include the prompt parameter in the authorization code request, and set the value to consent.
tl;博士 approval_prompt=在这种情况下需要强制才能获得刷新令牌。
详情:
access_type=如果想要获取刷新令牌,请求中始终需要离线。 (PierreDuc 的另一个答案不正确) 但有时您在交换您获得的 "authorization code" 后可能无法获得刷新令牌。 为什么? 为了 return 一个新的刷新令牌,我们希望用户同意。如果用户已经对您的应用程序授予离线同意,我们知道该应用程序已经有一个刷新令牌(至少一个)并且通常不应该一次又一次地请求它。所以我们不显示批准页面和 return 仅提供访问令牌的代码。
如果之前的刷新令牌不知何故丢失了,而您想再次获取它,那么在请求中添加 approval_prompt=force 将向用户显示一个批准页面,并且还会为您提供一个代码,该代码将提供刷新令牌。
这已经更新,必要的查询参数现在是 prompt=consent
见https://developers.google.com/identity/protocols/OAuth2WebServer#redirecting