停止从谷歌的API获取刷新令牌

时间:2014-07-02 17:50:28

标签: ruby-on-rails ruby ruby-on-rails-4 google-api google-drive-api

所以我有一个带有ruby on rails的网络应用程序连接到谷歌驱动器的API。到目前为止,它曾用于接收刷新令牌,因为我使用的其中一个参数是离线的。但最近它停止了它。正在发送的网址是:

Redirected to https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=ID&options=%7B:access_type=%3E%22offline%22,%20:approval_prompt=%3E%22force%22%7D&redirect_uri=http://localhost:3000/oauth2callbackdrive&response_type=code&scope=https://www.googleapis.com/auth/drive%20https://www.googleapis.com/auth/userinfo.email

重定向到URI的代码包含:

client = Google::APIClient.new
client.authorization.client_id = GOOGLE_CLIENT_ID
client.authorization.client_secret = GOOGLE_CLIENT_SECRET
client.authorization.scope = SCOPES
client.authorization.redirect_uri = GOOGLE_CLIENT_REDIRECT_URI
auth_url = client.authorization.authorization_uri(:options => {:access_type => :offline, :approval_prompt => :force}.to_s)
redirect_to auth_url.to_s

在回调函数上我有这个:

client = Google::APIClient.new
client.authorization.client_id = GOOGLE_CLIENT_ID
client.authorization.client_secret = GOOGLE_CLIENT_SECRET
client.authorization.scope = SCOPES
client.authorization.redirect_uri = GOOGLE_CLIENT_REDIRECT_URI
client.authorization.code = params['code']          
client.authorization.fetch_access_token!

在我插入数据库的同一个函数中:

CloudAccount.create(username: username,access_token:client.authorization.access_token, refresh_token:client.authorization.refresh_token,register_id:userID,cloud_name:'drive')

问题是,之前有值的client.authorization.refresh_token现在返回空,因此它作为NULL插入到数据库中。这会影响access_token到期时的上传/刷新/下载。

我能做什么或做错什么?

感谢。

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题(Getting a refresh token from google api)。我想如果你使用:         prompt='select_account consent'

然后强制一个新的批准屏幕,你得到一个新的refresh_token。

参考:https://developers.google.com/identity/protocols/OAuth2UserAgent