我们正在创建一个使用Ionic框架作为前端和Ruby on Rails作为后端的应用程序。我们可以在我们的应用程序中链接Gmail帐户。帐户链接工作正常,我们从前端获得serverAuthCode,然后使用该刷新令牌,我们能够在第一次尝试时获取具有该刷新令牌的电子邮件。但在几秒钟内,它就会过期或被撤销。遇到以下问题:
Signet::AuthorizationError (Authorization failed. Server message:
{
"error" : "invalid_grant",
"error_description" : "Token has been expired or revoked."
})
看起来,刷新令牌本身在几秒钟内就要过期了。有谁知道怎么解决这个问题吗?
更新:
现有代码如下所示:
class User
def authentication(linked_account)
client = Signet::OAuth2::Client.new(
authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
token_credential_uri: Rails.application.secrets.token_credential_uri,
client_id: Rails.application.secrets.google_client_id,
client_secret: Rails.application.secrets.google_client_secret,
scope: 'https://www.googleapis.com/auth/gmail.readonly, https://www.googleapis.com/auth/userinfo.email, https://www.googleapis.com/auth/userinfo.profile',
redirect_uri: Rails.application.secrets.redirect_uri,
refresh_token: linked_account[:refresh_token]
)
client.update!(access_token: linked_account.token, expires_at: linked_account.expires_at)
return AccessToken.new(linked_account.token) unless client.expired?
auth.fetch_access_token!
end
def get_email(linked_account)
auth = authentication(linked_account)
gmail = Google::Apis::GmailV1::GmailService.new
gmail.client_options.application_name = User::APPLICATION_NAME
gmail.authorization = AccessToken.new(linked_account.token)
query = "(is:inbox OR is:sent)"
gmail.list_user_messages(linked_account[:uid], q: "#{query}")
## Getting error over here ^^
end
end // class end
class AccessToken
attr_reader :token
def initialize(token)
@token = token
end
def apply!(headers)
headers['Authorization'] = "Bearer #{@token}"
end
end
参考链接:https://github.com/google/google-api-ruby-client/issues/296
https://stackoverflow.com/questions/50523052
复制相似问题