谷歌 oauth2 id_token 与 refresh_token

Posted

技术标签:

【中文标题】谷歌 oauth2 id_token 与 refresh_token【英文标题】:google oauth2 id_token vs refresh_token 【发布时间】:2014-11-21 09:23:09 【问题描述】:

我正在尝试使用 Google OAuth2 获取用户的联系信息。我并没有为获得访问权而苦苦挣扎,我想知道由于某种原因我已经停止获得refresh_token,而是获得了id_token长 JWT 字符串)。 我使用 python urllib 来检索用户的访问信息。我的代码是:

scope = 'https://accounts.google.com/o/oauth2/token'
params = urllib.urlencode(
    'code': request.GET['code'],
    'redirect_uri': settings.SOCIAL_AUTH_GOOGLE_REDIRECT_URI,
    'client_id': settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY,
    'client_secret': settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET,
    'grant_type': 'authorization_code',
)

回复:

u'access_token': u'hash',
 u'token_type': u'Bearer', 
 u'expires_in': 3600,
 u'id_token': u'really long hash'

我使用联系人范围https://www.google.com/m8/feeds/contacts/default/full?alt=json 当我尝试添加到参数access_type : offline 时,我收到以下错误:

Failed to retrive access_token. Status: 400
Message: 
  "error" : "invalid_request",
  "error_description" : "Parameter not allowed for this message type: access_type"

所以在那之后我想知道:

    我可以使用id_token 刷新我的access_token 吗? 如果第一个是True:如何? 获得身份验证的用户类型之间是否有任何差异,因为我注意到有时您会收到refresh_token,但我需要永久获得它,下次我制作 OAuth2 流程时,我获取id_token

【问题讨论】:

【参考方案1】:

如果您需要刷新令牌,最好将 access_type=offline 和approval_prompt=force 添加到https://accounts.google.com/o/oauth2/auth?

var url = 'https://accounts.google.com/o/oauth2/auth?' +
          'client_id=' + CLIENT_ID + '&' +
          'response_type=code&access_type=offline&approval_prompt=force&' +
          'redirect_uri=' + encodeURIComponent(REDIRECT_URL) +
          '&scope=' + SCOPES;

那么返回的代码总是会在下次与https://www.googleapis.com/oauth2/v4/token握手时给你一个刷新代码

【讨论】:

以上是关于谷歌 oauth2 id_token 与 refresh_token的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Postman 的 Oauth2 中使用 id_token 而不是 access_token?

澄清 id_token 与 access_token

OAuth 2 access_token vs OpenId Connect id_token

谷歌登录 - “access_token” vs “id_token” vs “code”

什么是 id_token google oauth

将 id_token 传递给浏览器代码是不是不安全?