Files
MatchLiveTv/backend/app/services/youtube/oauth_refresh.rb
Emiliano Frascaro 92c0058085 Corregge OAuth YouTube piattaforma senza access token in memoria.
Il refresh token ENV non veniva scambiato finché expires_at era vuoto,
causando 500 Unauthorized alla creazione sessione YouTube.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 22:43:31 +02:00

32 lines
919 B
Ruby

module Youtube
class OauthRefresh
TOKEN_URL = "https://oauth2.googleapis.com/token".freeze
def initialize(credential)
@credential = credential
end
def apply!(service)
refresh! if @credential.refresh_token.present? && (@credential.access_token.blank? || @credential.expired?)
service.authorization = @credential.access_token
service
end
def refresh!
response = Faraday.post(TOKEN_URL, {
client_id: ENV["YOUTUBE_CLIENT_ID"],
client_secret: ENV["YOUTUBE_CLIENT_SECRET"],
refresh_token: @credential.refresh_token,
grant_type: "refresh_token"
})
data = JSON.parse(response.body)
raise Youtube::BroadcastService::Error, data["error"] if data["error"]
@credential.update!(
access_token: data["access_token"],
expires_at: Time.current + data["expires_in"].to_i.seconds
)
end
end
end