【问题标题】:Yandex Oauth authorization code always expired, Ruby On RailsYandex Oauth 授权码总是过期,Ruby On Rails
【发布时间】:2017-08-31 03:54:58
【问题描述】:

我按照guide 在我的 Ruby On Rails 应用程序中实现了 Yandex Oauth。几周后我没有遇到任何问题,但最近(几天前)我遇到了问题。我无法获取访问令牌、刷新令牌等,因为我的请求失败并出现 400 错误代码。

我收到这条特定消息:

{"error_description": "Code has expired", "error": "invalid_grant"}

来自指南:

此代码的生命周期为 10 分钟。到期时,必须提供代码 再次请求。

但它永远不会等待 10 分钟甚至 10 秒,因为一旦我的应用程序获得授权码,它就会立即发出 POST 请求来更改此授权码以获取访问令牌、刷新令牌和过期。但是此 POST 请求失败,因为该授权代码似乎已过期。

来自 Yandex 错误描述:

invalid_grant ― 无效或过期的授权码。

我的代码:

def yandex
    require 'net/http'
    require 'json'  # => false

    @user = User.from_omniauth(request.env["omniauth.auth"])

    @client_id = Rails.application.secrets.client_id 
    @secret =  Rails.application.secrets.password
    @authorization_code = params[:code]

    @user.update_attribute(:code, @authorization_code)
    @user.update_attribute(:state, params[:state])


    @post_body = "grant_type=authorization_code&code=#{@authorization_code}&client_id=#{@client_id}&client_secret=#{@secret}"

    @url = "https://oauth.yandex.ru/token"

    url = URI.parse(@url)
    req = Net::HTTP::Post.new(url.request_uri)
    req['host'] ="oauth.yandex.ru"
    req['Content-Length'] = @post_body.length
    req['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = @post_body
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = (url.scheme == "https")

    @response_mess = http.request(req)

    refreshhash = JSON.parse(@response_mess.body)
    access_token  = refreshhash['access_token']
    refresh_token  = refreshhash['refresh_token']
    access_token_expires_at = DateTime.now + refreshhash["expires_in"].to_i.seconds


    if access_token.present? && refresh_token.present? && access_token_expires_at.present?
        @user.update_attribute(:access_token, access_token)
        @user.update_attribute(:refresh_token, refresh_token)
        @user.update_attribute(:expires_in, access_token_expires_at)

        sign_in(@user)
        redirect_to admin_dashboard_index_path
    end

end

我的日志:

Started GET "/users/auth/yandex/callback?state=31c11a86beecdeb55ab30887d56391a8cbafccdc85c456aa&code=5842278" for 212.3.192.116 at 2017-04-05 15:58:27 +0300
Cannot render console from 212.3.192.116! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by CallbacksController#yandex as HTML
  Parameters: {"state"=>"31c11a86beecdeb55ab30887d56391a8cbafccdc85c456aa", "code"=>"5842278"}

我在 CHROME POSTMAN 中尝试了相同的 POST 请求,并收到了与 {"error_description": "Code has expired", "error": "invalid_grant"} 相同的响应

我已经用谷歌搜索了,但没有任何类似的问题.. 问题似乎出在 Yandex 端,但是如何解决呢?

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby oauth yandex yandex-api


    【解决方案1】:

    Yandex 授权码是一次性的。你确定你没有向“https://oauth.yandex.ru/token”发出两次请求吗?您可以评估执行相同操作的代码之前尝试在 Chrome Postman 中发出此类请求。

    【讨论】:

    • 我在 Chrome Postman 中尝试过,我收到的响应与在我的 Rails 应用程序中相同,它是“代码已过期”。
    • 当你获得授权码时你使用什么client_id?是否等于Rails.application.secrets.client_id
    • 是的,我使用了相同的 client_id。如果我更改了几个符号(用于调试),则返回的请求找不到此类客户端或客户端不存在。具体记不太清了。。
    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 2017-05-27
    • 2012-10-23
    • 2019-08-03
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多