重新验证时错过了Google刷新令牌

时间:2018-10-03 07:41:18

标签: ruby-on-rails google-api google-calendar-api

起初,我可以通过Google认证获得refresh token,但是再次进行认证时却无法获得。再次执行时,也会跳过对Google日历屏幕的确认。跟随这张照片。

enter image description here

起初我会变得这样。

  

{“提供者”:“ google_oauth2”,“ uid”:“ XXXXXXXXXXXXXXX”,“名称”:“ XXXXXXXXX”,“电子邮件”:“ XXXXXXXXXXXX”,“图像”:{“ URL”:“ /上传/用户/image/2/photo.jpg"},"access_token":"XXXXXXXXX","re​​fresh_token":"XXXXXXXXXXXXXXXXXXX“,” oauth_expires_at“:” 2018-10-03T08:20:22.000Z“,” id“:2, “ created_at”:“ 2018-10-01T11:38:25.422Z”,“ updated_at”:“ 2018-10-03T07:20:23.829Z”}

但同样,refresh_tokennull

  

{“提供者”:“ google_oauth2”,“ uid”:“ XXXXXXXXXXXXXXX”,“名称”:“ XXXXXXXXX”,“电子邮件”:“ XXXXXXXXXXXX”,“图像”:{“ URL”:“ /上传/用户/image/2/photo.jpg"},"access_token":"XXXXXXXXX","re​​fresh_token":null,"oauth_expires_at":"2018-10-03T08:20:22.000Z","id":2,"created_at “:” 2018-10-01T11:38:25.422Z“,” updated_at“:” 2018-10-03T07:20:23.829Z“}   ``

user.rb

  def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
  user.provider = auth.provider
  user.uid = auth.uid
  user.name = auth.info.name
  user.email = auth.info.email
  user.remote_image_url = auth.info.image
  user.access_token = auth.credentials.token
  user.refresh_token = auth.credentials.refresh_token
  user.oauth_expires_at = Time.at(auth.credentials.expires_at)
  return user
end

结束

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2,
  Rails.application.credentials.google[:google_client_id],
  Rails.application.credentials.google[:google_client_secret],
  {
    :provider_ignores_state => true,
    image_size: 150,
    scope: %w(https://www.googleapis.com/auth/userinfo.email     https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar).join(' '),
    prompt: 'consent',
    access_type: 'offline'
  }
end

1 个答案:

答案 0 :(得分:0)

Google并不总是返回带有Web凭证的刷新令牌。 Google假设您在第一次请求用户访问时保存了刷新令牌,并将在以后的所有请求中使用它。

如果您想找回刷新令牌,则应该转到用户google permissions,删除该应用程序,然后再次请求。

注意:re-consent可能也可以工作,我不记得您可能想尝试一下。请注意,这将需要用户同意您的应用程序才能再次访问。