Last active
January 31, 2024 21:22
-
-
Save iProdigy/a3ef49d882c6d0db7cb88f9a6360ee55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final String clientId = "***", clientSecret = "***", redirectUrl = null; | |
final TwitchIdentityProvider tip = new TwitchIdentityProvider(clientId, clientSecret, redirectUrl); | |
Supplier<OAuth2Credential> readCredentialFromFile = () -> { | |
final String initialAccessToken = "***", initialRefreshToken = "***"; // TODO: must be read from file | |
final OAuth2Credential cred = new OAuth2Credential("twitch", initialAccessToken); | |
cred.setRefreshToken(initialRefreshToken); | |
return cred; | |
}; | |
Consumer<OAuth2Credential> saveCredentialToFile = token -> { | |
// TODO: save to the same file | |
}; | |
final OAuth2Credential initialToken = readCredentialFromFile.get(); | |
OAuth2Credential credential = tip.getAdditionalCredentialInformation(initialToken) | |
.orElseGet(() -> tip.refreshCredential(initialToken).flatMap(tip::getAdditionalCredentialInformation).orElse(null)); | |
if (credential == null) { | |
throw new RuntimeException("Invalid token!"); | |
} else if (credential != initialToken) { | |
saveCredentialToFile.accept(credential); | |
} | |
ScheduledThreadPoolExecutor exec = ThreadUtils.getDefaultScheduledThreadPoolExecutor("twitch4j", Runtime.getRuntime().availableProcessors()); | |
if (credential.getExpiresIn() > 0) { | |
exec.scheduleAtFixedRate(() -> tip.refreshCredential(credential).ifPresent(cred -> { | |
credential.updateCredential(cred); | |
saveCredentialToFile.accept(credential); | |
}), credential.getExpiresIn() / 2, Duration.ofHours(1L).getSeconds(), TimeUnit.SECONDS); | |
} | |
CredentialManager credentialManager = CredentialManagerBuilder.builder().build(); | |
credentialManager.registerIdentityProvider(tip); | |
ITwitchClient twitchClient = TwitchClientPoolBuilder.builder() | |
.withClientId(clientId) | |
.withClientSecret(clientSecret) | |
.withRedirectUrl(redirectUrl) | |
.withCredentialManager(credentialManager) | |
.withChatAccount(credential) | |
.withDefaultAuthToken(credential) | |
.withEnableChat(true) | |
.withEnableHelix(true) | |
.withEnableChatPool(true) | |
.withScheduledThreadPoolExecutor(exec) | |
.build(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment