Files
MatchLiveTv/backend/app/services/youtube/oauth_exchange.rb
Emiliano Frascaro 71eaf9cf56 Fix callback OAuth YouTube: pagina HTML con refresh token invece di risposta API vuota.
Il codice OAuth va usato una sola volta; messaggio chiaro se si ricarica il callback.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 19:57:08 +02:00

33 lines
1.0 KiB
Ruby

module Youtube
class OauthExchange
TOKEN_URL = "https://oauth2.googleapis.com/token".freeze
def self.call(code)
response = Faraday.post(TOKEN_URL, {
code: code,
client_id: ENV["YOUTUBE_CLIENT_ID"],
client_secret: ENV["YOUTUBE_CLIENT_SECRET"],
redirect_uri: ENV["YOUTUBE_REDIRECT_URI"],
grant_type: "authorization_code"
})
data = JSON.parse(response.body)
if data["error"]
msg = data["error_description"].presence || data["error"]
if data["error"] == "invalid_grant"
msg = "Codice OAuth già usato o scaduto. Riapri /admin/youtube/platform e ripeti il collegamento."
end
raise BroadcastService::Error, msg
end
info = ChannelInfo.fetch(access_token: data["access_token"])
{
access_token: data["access_token"],
refresh_token: data["refresh_token"],
expires_in: data["expires_in"].to_i,
channel_id: info[:channel_id],
channel_title: info[:channel_title]
}
end
end
end