【问题标题】:google oauth2 id_token vs refresh_token谷歌 oauth2 id_token 与 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"
}

所以在那之后我想知道:

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

【问题讨论】:

    标签: python oauth-2.0 google-contacts-api jwt


    【解决方案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握手时给你一个刷新代码

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2016-10-25
      • 2013-09-05
      • 2014-07-11
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      相关资源
      最近更新 更多