module Api module V1 class YoutubeController < BaseController def authorize team = current_user.manageable_teams.find(params[:team_id]) team.entitlements.assert_can_connect_youtube! url = Youtube::OauthUrl.build(team_id: team.id, redirect_uri: ENV.fetch("YOUTUBE_REDIRECT_URI")) render json: { authorization_url: url } end def callback team = current_user.manageable_teams.find(params[:state]) tokens = Youtube::OauthExchange.call(params[:code]) cred = team.youtube_credential || team.build_youtube_credential cred.update!( access_token: tokens[:access_token], refresh_token: tokens[:refresh_token], expires_at: Time.current + tokens[:expires_in].seconds, channel_id: tokens[:channel_id], channel_title: tokens[:channel_title] ) redirect_to "/admin/teams/#{team.id}?youtube=connected" end end end end