简体   繁体   English

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

[英]Google refresh token missed when authenticating again

At first I could get refresh token on Google authentication but couldn't get it when authenticating again. 起初,我可以在Google身份验证上获取refresh token ,但在再次进行身份验证时却无法获取refresh token When I do again, confirmation of Google calendar screen is skipped also. 再次执行时,也会跳过对Google日历屏幕的确认。 following this picture. 跟随这张照片。

在此处输入图片说明

At first I could get like this. 起初我会得到这样的。

{"provider":"google_oauth2","uid":"XXXXXXXXXXXXXXX","name":"XXXXXXXXX","email":"XXXXXXXXXXXX","image":{"url":"/uploads/user/image/2/photo.jpg"},"access_token":"XXXXXXXXX","refresh_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"} { “提供者”: “google_oauth2”, “UID”: “XXXXXXXXXXXXXXX”, “名”: “XXXXXXXXX”, “电子邮件”: “XXXXXXXXXXXX”, “形象”:{ “URL”:“/上传/用户/图片/ 2 / photo.jpg“},” access_token“:” XXXXXXXXX“,” refresh_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”}

But again, refresh_token is null . 但同样, refresh_tokennull

{"provider":"google_oauth2","uid":"XXXXXXXXXXXXXXX","name":"XXXXXXXXX","email":"XXXXXXXXXXXX","image":{"url":"/uploads/user/image/2/photo.jpg"},"access_token":"XXXXXXXXX","refresh_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"} ```` { “提供者”: “google_oauth2”, “UID”: “XXXXXXXXXXXXXXX”, “名”: “XXXXXXXXX”, “电子邮件”: “XXXXXXXXXXXX”, “形象”:{ “URL”:“/上传/用户/图片/ 2 / photo.jpg “},” 的access_token “:” XXXXXXXXX “ ”refresh_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 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

end 结束

omniauth.rb omn​​iauth.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

Google does not always return a refresh token with a web credentials. Google并不总是返回带有Web凭证的刷新令牌。 Google assumes that when you requested access of the user the first time that you saved the refresh token and will be using that in all future requests. Google假设您在第一次请求用户访问时保存了刷新令牌,并将在以后的所有请求中使用它。

If you want to get a refresh token back you should go to the users google permissions remove the application and then request again. 如果您想找回刷新令牌,则应转到用户google 权限,删除该应用程序,然后再次请求。

Note: re-consent may also work i cant remember you might want to try that. 注意: 重新同意也可能有效,我不记得您可能想尝试一下。 Note this is going to require that the user consent to your application having access again. 请注意,这将需要用户同意您的应用程序才能再次访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM