Ruby On Rails 앱에서 Yandex Oauth를 구현하기 위해이 guide을 따랐습니다. 몇 주 동안 문제가 없었지만 최근에 (며칠 전) 문제가 발생했습니다. 내 요청이 400 오류 코드로 실패하기 때문에 액세스 토큰, 토큰 새로 고침 등을 얻을 수 없습니다.Yandex Oauth 인증 코드가 항상 만료되었습니다. Ruby On Rails
나는이 특정 메시지를 수신하고 있습니다 : 가이드에서
{"error_description": "Code has expired", "error": "invalid_grant"}
:
이이 코드의 수명은 10 분입니다. 만료되면 코드를 다시 요청해야합니다.
그러나 곧 내 응용 프로그램이 인증 코드를수록 즉시 액세스 토큰이 인증 코드를 변경하는 POST 요청을 수 있기 때문에 결코 10 분, 심지어 10 초 대기되지는 토큰 및 만료 새로 고칩니다. 그러나이 인증 요청 코드는 만료 된 것으로 보이므로이 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 "https://stackoverflow.com/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"}
내가 크롬 POSTMAN에서 동일한 POST 요청을 시도하고
{"error_description": "Code has expired", "error": "invalid_grant"}
나는이 같은 응답을 받았습니다 주위에 인터넷 검색하지만 비슷한 질문이 없습니다 .. 그것은 문제가 보인다 Yandex 끝이지만, 어떻게 돌아 다니는거야?
도움이 될 것입니다.
Chrome Postman을 사용해 보았는데 Rails 앱과 동일한 응답을 받았으며 "코드가 만료되었습니다." – Edgars
그리고 authorization_code를 얻을 때 어떤 client_id를 사용합니까? 'Rails.application.secrets.client_id'와 같은가요? –
예, 동일한 client_id를 사용했습니다. 디버깅을 위해 몇 가지 기호를 변경 한 경우 반환 된 요청은 해당 클라이언트를 찾을 수 없거나 클라이언트가 존재하지 않습니다. 정확히 기억이 안나 .. – Edgars