Class YouTubeAuthHelper

java.lang.Object
uno.anahata.asi.yam.tools.youtube.YouTubeAuthHelper

public class YouTubeAuthHelper extends Object
Pure Java Google OAuth 2.0 helper managing browser login, authorization code capture, and background access token refresh for YouTube Data API v3.

Operates with zero external OAuth dependencies by utilizing HttpServer for local callback interception, Desktop for system browser launch, and HttpClient for token exchanges.

Author:
anahata
  • Field Details

    • GOOGLE_AUTH_ENDPOINT

      private static final String GOOGLE_AUTH_ENDPOINT
      Google OAuth 2.0 user authorization endpoint URL.
      See Also:
    • GOOGLE_TOKEN_ENDPOINT

      private static final String GOOGLE_TOKEN_ENDPOINT
      Google OAuth 2.0 token exchange and refresh endpoint URL.
      See Also:
    • YOUTUBE_SCOPES

      private static final String YOUTUBE_SCOPES
      Space-delimited list of YouTube Data API v3 OAuth scopes required for uploading and managing videos.
      See Also:
    • DEFAULT_CLIENT_ID

      public static final String DEFAULT_CLIENT_ID
      Default official Anahata ASI Desktop Google Cloud OAuth 2.0 Client ID.
    • DEFAULT_CLIENT_SECRET

      public static final String DEFAULT_CLIENT_SECRET
      Default official Anahata ASI Desktop Google Cloud OAuth 2.0 Client Secret.
    • PREFERRED_PORT

      private static final int PREFERRED_PORT
      Preferred local port for the ephemeral OAuth callback HTTP server.
      See Also:
    • CALLBACK_PATH

      private static final String CALLBACK_PATH
      Local path context where Google OAuth redirects with the authorization code.
      See Also:
    • MAPPER

      private static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
      Shared JSON object mapper for deserializing Google token responses.
    • HTTP_CLIENT

      private static final HttpClient HTTP_CLIENT
      Shared HTTP client configured with a 15-second connect timeout for OAuth exchanges.
    • cachedAccessToken

      private static String cachedAccessToken
      Cached in-memory access token.
    • tokenExpiry

      private static Instant tokenExpiry
      Expiration instant of the cached access token.
  • Constructor Details

    • YouTubeAuthHelper

      public YouTubeAuthHelper()
  • Method Details

    • getValidAccessToken

      public static String getValidAccessToken(YouTubeCredentials credentials) throws IOException
      Obtains a valid, unexpired OAuth2 access token.

      If the cached token is expired or missing, it automatically refreshes the token using the stored refresh_token via Google's token endpoint.

      Parameters:
      credentials - The loaded YouTubeCredentials.
      Returns:
      A valid OAuth2 bearer access token.
      Throws:
      IOException - If the token request or refresh fails.
    • loginInteractive

      public static YouTubeCredentials loginInteractive(String clientId, String clientSecret, String playlistId) throws Exception
      Executes the interactive 1-click browser login flow.

      1. Launches a temporary local HTTP server on port 8888.
      2. Opens the user's default browser to Google's OAuth consent screen.
      3. Intercepts the authorization code from the redirect callback.
      4. Exchanges the code for a permanent refresh_token and access token.
      5. Saves credentials to ~/.anahata/asi/youtube/credentials.json.

      Parameters:
      clientId - The Google Cloud OAuth Client ID.
      clientSecret - The Google Cloud OAuth Client Secret.
      playlistId - The optional default playlist ID.
      Returns:
      The authenticated and saved YouTubeCredentials.
      Throws:
      Exception - If authorization or token exchange fails.