ruby-on-rails - 获取无法刷新 google_oauth2 身份验证令牌错误。
问题描述
我正在尝试从 OAuth.io 为任何基于 Google 的提供商获取访问令牌,但是每当我进行身份验证时,我都会获得 access_token 但没有 refresh_token。我为 access_type 选择了离线,但仍然没有乐趣。
def google_auth
# Create a new API client & load the Google Drive API
client = Google::APIClient.new
client.authorization.client_id = ENV['GOOGLE_ID']
client.authorization.client_secret = ENV['GOOGLE_SECRET']
client.authorization.scope = ENV['GOOGLE_SCOPE']
client.authorization.redirect_uri = ENV['REDIRECT_URI']
client.authorization.code = self.code.chomp || ""
client.authorization.access_token = self.token
client.authorization.refresh_token = self.refresh_token
# client.authorization.additional_parameters = {
# "access_type" => "offline", # offline access
# "include_granted_scopes" => "true" # incremental auth
#
if client.authorization.refresh_token &&
client.authorization.expired?
client.authorization.fetch_access_token!
end
return client
end
def refresh_google
options = {
body: {
client_id: ENV['GOOGLE_ID'],
client_secret: ENV['GOOGLE_SECRET'],
refresh_token: self.refresh_token,
grant_type: 'refresh_token',
access_type: 'offline'
},
headers: {
'Content-Type' => 'application/x-www-form-urlencoded',
# 'access_type' =>'offline'
}
}
@response = HTTParty.post('https://accounts.google.com/o/oauth2/token', options)
if @response.code == 200
self.token = @response.parsed_response['access_token']
self.save
else
Rails.logger.error("Unable to refresh google_oauth2 authentication token.")
Rails.logger.error("Refresh token response body: #{@response.body}")
end
end
请在这方面提供帮助
解决方案
Issue resolved: I was trying to put
"client.authorization.additional_parameters = {
"access_type" => "offline", # offline access
"include_granted_scopes" => "true" # incremental auth"
in User the rb model where callback was made but had to put it under session controller where API was called!
推荐阅读
- c# - ASP.NET:如何在不更改浏览器中的 URL 的情况下进行重定向?
- elasticsearch - 删除后正在重新创建 K8S 中的状态集
- python - 如何在python中的列中使用if条件
- c# - 如何使用 Accord 在组合框中设置不同的相机分辨率?
- python - 如何使用列表中的数字作为比较的基准
- javascript - 这里映射javascript事件pointerenter和pointerleave不适用于组
- python - openpyxl:如何为轴设置参考
- reactjs - CSS不适用于反应js中的嵌套路由
- mysql - 如何使用 mysql 空间索引找到前 5 个最近点
- unreal-engine4 - 有没有办法让阴影被点亮而其他一切都变暗?