微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OAuth2:RuntimeError:刷新令牌不可用

如何解决OAuth2:RuntimeError:刷新令牌不可用

我使用OAuth2 :: Client获取access_token。 需要使用refresh_token刷新它

client_id = '95585XXXXXXXoogleercontent.com'
secret_key = 'R10Ze490IYa'
client = OAuth2::Client.new(client_id,secret_key,{
  authorize_url: 'https://accounts.google.com/o/oauth2/auth',token_url: 'https://accounts.google.com/o/oauth2/token'
})


redirect_url = client.auth_code.authorize_url({
  scope: 'https://www.googleapis.com/auth/analytics.readonly',redirect_uri: 'https://example.com',access_type: 'offline'
})

auth_code = '4/5AF6VI0JMcmA38XXXXX' # getted from redirect_url clicking 

access_token = client.auth_code.get_token(auth_code,redirect_uri: 'https://example.com')

然后我尝试刷新令牌:

access_token.refresh!

我有错误

运行时错误:refresh_token不可用

解决方法

需要添加-提示:“同意”:

  redirect_url = client.auth_code.authorize_url({
    scope: 'https://www.googleapis.com/auth/analytics.readonly',redirect_uri: 'https://example.com',access_type: 'offline',prompt: 'consent'
  })

然后您可以获得新的access_token:

new_access_token = access_token.refresh!

您还可以使用以下方法保存令牌以供将来使用:

hash = access_token.to_hash  # save it
# load:
loaded_token = OAuth2::AccessToken.from_hash(client,hash)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。