2014-09-26 94 views
1

我正在嘗試使用Google OAuth2獲取用戶的聯繫信息。我不努力獲得訪問權限,我想知道由於某種原因,我已經停止獲取refresh_token,而是獲得id_token長JWT字符串)。 我使用python urllib來檢索用戶的訪問信息。我的代碼是:google oauth2 id_token vs refresh_token

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 當我試圖添加到PARAMS 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

回答

0

如果你需要一個刷新令牌,你最好ACCESS_TYPE =離線和approval_prompt =力添加到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; 

則返回的代碼將永遠給你與的下一個握手刷新代碼