Class YouTubeAuthHelper
java.lang.Object
uno.anahata.asi.yam.tools.youtube.YouTubeAuthHelper
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static StringCached in-memory access token.private static final StringLocal path context where Google OAuth redirects with the authorization code.static final StringDefault official Anahata ASI Desktop Google Cloud OAuth 2.0 Client ID.static final StringDefault official Anahata ASI Desktop Google Cloud OAuth 2.0 Client Secret.private static final StringGoogle OAuth 2.0 user authorization endpoint URL.private static final StringGoogle OAuth 2.0 token exchange and refresh endpoint URL.private static final HttpClientShared HTTP client configured with a 15-second connect timeout for OAuth exchanges.private static final com.fasterxml.jackson.databind.ObjectMapperShared JSON object mapper for deserializing Google token responses.private static final intPreferred local port for the ephemeral OAuth callback HTTP server.private static InstantExpiration instant of the cached access token.private static final StringSpace-delimited list of YouTube Data API v3 OAuth scopes required for uploading and managing videos. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetValidAccessToken(YouTubeCredentials credentials) Obtains a valid, unexpired OAuth2 access token.static YouTubeCredentialsloginInteractive(String clientId, String clientSecret, String playlistId) Executes the interactive 1-click browser login flow.
-
Field Details
-
GOOGLE_AUTH_ENDPOINT
Google OAuth 2.0 user authorization endpoint URL.- See Also:
-
GOOGLE_TOKEN_ENDPOINT
Google OAuth 2.0 token exchange and refresh endpoint URL.- See Also:
-
YOUTUBE_SCOPES
Space-delimited list of YouTube Data API v3 OAuth scopes required for uploading and managing videos.- See Also:
-
DEFAULT_CLIENT_ID
Default official Anahata ASI Desktop Google Cloud OAuth 2.0 Client ID. -
DEFAULT_CLIENT_SECRET
Default official Anahata ASI Desktop Google Cloud OAuth 2.0 Client Secret. -
PREFERRED_PORT
private static final int PREFERRED_PORTPreferred local port for the ephemeral OAuth callback HTTP server.- See Also:
-
CALLBACK_PATH
Local path context where Google OAuth redirects with the authorization code.- See Also:
-
MAPPER
private static final com.fasterxml.jackson.databind.ObjectMapper MAPPERShared JSON object mapper for deserializing Google token responses. -
HTTP_CLIENT
Shared HTTP client configured with a 15-second connect timeout for OAuth exchanges. -
cachedAccessToken
Cached in-memory access token. -
tokenExpiry
Expiration instant of the cached access token.
-
-
Constructor Details
-
YouTubeAuthHelper
public YouTubeAuthHelper()
-
-
Method Details
-
getValidAccessToken
Obtains a valid, unexpired OAuth2 access token.If the cached token is expired or missing, it automatically refreshes the token using the stored
refresh_tokenvia Google's token endpoint.- Parameters:
credentials- The loadedYouTubeCredentials.- 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 permanentrefresh_tokenand 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.
-

