Last active
November 27, 2022 02:51
-
-
Save iProdigy/8221cc37db1970cb05e2681946c63828 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
package com.github.twitch4j; | |
import com.github.philippheuer.credentialmanager.domain.OAuth2Credential; | |
import com.github.twitch4j.auth.providers.TwitchIdentityProvider; | |
import com.github.twitch4j.common.util.ThreadUtils; | |
import java.util.Collections; | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
public class RefreshingAppBot { | |
private final String clientId = "client-id", clientSecret = "client-secret"; | |
private final TwitchIdentityProvider tip = new TwitchIdentityProvider(clientId, clientSecret, null); | |
private final OAuth2Credential credential = tip.getAppAccessToken(); | |
private final ITwitchClient twitchClient; | |
private RefreshingAppBot() { | |
// Schedule task to refresh in the future | |
final ScheduledThreadPoolExecutor exec = ThreadUtils.getDefaultScheduledThreadPoolExecutor("exec", Runtime.getRuntime().availableProcessors() * 2); | |
exec.scheduleAtFixedRate( | |
() -> credential.updateCredential(tip.getAppAccessToken()), | |
1L, | |
1L, | |
TimeUnit.DAYS | |
); | |
// Build api | |
twitchClient = TwitchClientBuilder.builder() | |
.withClientId(clientId) | |
.withClientSecret(clientSecret) | |
.withDefaultAuthToken(credential) | |
.withScheduledThreadPoolExecutor(exec) | |
.withEnableHelix(true) | |
.build(); | |
} | |
public void doExampleQuery() { | |
twitchClient.getHelix().getUsers(null, null, Collections.singletonList("ogprodigy")).execute(); | |
} | |
public static void main(String[] args) { | |
RefreshingAppBot bot = new RefreshingAppBot(); | |
bot.doExampleQuery(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment