首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google::Apis::AuthorizationError (未经授权)

Google::Apis::AuthorizationError (未经授权)
EN

Stack Overflow用户
提问于 2018-05-25 14:46:36
回答 6查看 4.3K关注 0票数 19

我们正在创建一个使用Ionic框架作为前端和Ruby on Rails作为后端的应用程序。我们可以在我们的应用程序中链接Gmail帐户。帐户链接工作正常,我们从前端获得serverAuthCode,然后使用该刷新令牌,我们能够在第一次尝试时获取具有该刷新令牌的电子邮件。但在几秒钟内,它就会过期或被撤销。遇到以下问题:

代码语言:javascript
复制
Signet::AuthorizationError (Authorization failed.  Server message:
{
  "error" : "invalid_grant",
  "error_description" : "Token has been expired or revoked."
})

看起来,刷新令牌本身在几秒钟内就要过期了。有谁知道怎么解决这个问题吗?

更新:

现有代码如下所示:

代码语言:javascript
复制
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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50523052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档