Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DWX24: Twitch4J – Coffee and Games

DWX24: Twitch4J – Coffee and Games

Twitch4J is a Java and JVM-language based API client for the Twitch REST API interfaces including Helix, Chat, Events, PubSub or GraphQL.
It supports Java, Kotlin and Groovy. Twitch4J provides three ways to execute requests against the Twitch API and process the results:
- Synchronous execution, executing API calls synchronously via execute().
- Asynchronous execution, placing calls asynchronously with the queue() method.
- Reactive execution, observing the results of any Twitch API call as an Observable.

This session provides an overview of the Twitch4J features, how to use them with Java, Kotlin and Groovy in a synchronous, asynchronous or a reactive programming style. Followed by hands-on examples of a Twitch bot and streaming server in each supported language, as well as integration with other systems like Streamlabs, Discord, Minecraft or Spotify.

Avatar for Werner Keil

Werner Keil

July 07, 2024
Tweet

More Decks by Werner Keil

Other Decks in Programming

Transcript

  1. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Agenda 1. Introduction 2. Standards & Specifications 3. Twitch4J 4. Demos 5. Links & Resources 6. Q&A
  2. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Who am I? Werner Keil • Consultant – Coach • Creative Cosmopolitan • Open Source Evangelist • Software Architect • Author, Speaker • Maintenance Lead – JSR 354, 385 • Jakarta EE Spec Committee Member [www.linkedin.com/in/catmedia]
  3. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Twitch Founded in 2011, Twitch is a platform for live video streaming, offering nearly anything you’d want to watch, from cooking, music, live coding, Q&A sessions, and, the leading portion of traffic: video games. It started as a spinoff division of Justin.tv, with gaming as the key priority. Twitch became so popular it led to an entire rebrand of Justin.tv, which changed its name to Twitch Interactive in 2014. Then, later that year, Twitch Interactive was acquired by Amazon. History
  4. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Twitch Like “Youtubers” and other Social Media Influencers, some Twitch Streamers can sometimes reach fame and fortune, if their content attracts Millions of followers, which also leads to 6 or 7-digit annual income for some of the top content creators. Stars
  5. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Twitch Top Earners 10 Twitch Top Earners in 2023 1. Ninja 2. Auronplay 3. Rubius 4. ibai 5. xQc 6. TFue 7. The Grefg 8. Shroud 9. juansguarnizo 10. Pokimane
  6. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Standards @wernerwedge
  7. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook OAuth OAuth is a protocol to delegate rights for an application to act on behalf of a user who granted its rights without giving away their login / password Developed by Twitter, Magnolia and Google, it was made standard by IETF in April 2010 under RFC 5849 Version 2.0, simpler to use but often criticized by its too many implementations was standardized in October 2012 under RFC 6749 and 6750. It’s already used by many actors (Social Networks like Facebook, Google, Microsoft as well as most API providers ) History
  8. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook OAuth Overview • An Authorization/Delegation Framework • Standardized by RFC6749 ̶ RFC 6750 using bearer tokens ̶ RFC 6819 Security considerations • On a foundation of Token standards ̶ JSON Object Signing Encryption (JOSE) ̶ JWT (RFC7519), JWS (RFC7515), JWE (RFC7516), JWA (RFC7518), JWK (RFC7517)
  9. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook OAuth Concepts • Actors ̶ Resource Owner ̶ Client ̶ Resource, Resource server ̶ Authorization Server • Authorizations represented as ‘scopes’
  10. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook OAuth Dance Creating an application in the OAuth service Initialization: the right granting phase also called the OAuth Dance. At the end of the dance we obtain an access token (formed by a public and secret part) for the next step Signature: each request is signed with access token and token identifying the OAuth application that was granted the rights OAuth has 3 steps
  11. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook OpenID Connect (OIDC) Overview • Authentication Protocol built on OAuth2 • Session Management – Single Sign on, Out • An additional Token Type – ID Token • UserInfo, Discovery, Client Self-registration Endpoints • Specs : OpenID core, Discovery, Client Registration
  12. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Specifications @wernerwedge
  13. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Jakarta Security Creating Secure Applications • Standardize Terminology • API for Authentication mechanism • API for Identity Store • API for Security Context • API for Role/Permission Assignment
  14. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Security – Identity Store Overview • Abstract the Identity Store used by an application • Simple configuration • Support a variety of Identity stores ̶ Lightweight k-v development stores ̶ Traditional stores – LDAP, DB ̶ Cloud-specific stores e.g. Social Logins, 3rd-party Cloud Identity providers
  15. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Security – Identity Store Features • Orderable to support multiple identity stores • Abstraction to support variety of credential types like ̶ Username/Password ̶ OAuth Client ID & Secret or JWT Tokens • Consistent API regardless of container • Enables Application to determine ̶ User’s identity ̶ Identity Provider that was used to establish identity ̶ Which groups or roles the user belongs to
  16. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Security – Context Definition // Security Context public interface SecurityContext{ Principal getCallerPrincipal(); <T extends Principal> Set<T> getPrincipalsByType(Class<T> pType); boolean isCallerInRole(String role); boolean hasAccessToWebResource(String resource, String... methods); AuthenticationStatus authenticate(HttpServletRequest request, HttpServletResponse response, AuthenticationParameters parameters); }
  17. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Security – Context Testing for Access // Consider the following Servlet definition @WebServlet("/protectedServlet") @ServletSecurity(@HttpConstraint(rolesAllowed = "foo")) public class ProtectedServlet extends HttpServlet { ... } // And the following call to hasAccessToWebResource() securityContext.hasAccessToWebResource("/protectedServlet", GET) Returns true only if the caller is in role "foo".
  18. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Twitch4J @wernerwedge
  19. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Twitch4J Overview Twitch4J is a Java and JVM-based API client for the Twitch REST API Interfaces. It supports Java, Kotlin and Groovy. The project contains several modules that can be used independently or together: • Twitch Kraken API • Twitch Helix API • Twitch IRC • Twitch PubSub
  20. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook // Load Configuration loadConfiguration(); TwitchClientBuilder clientBuilder = TwitchClientBuilder.builder(); // region Auth OAuth2Credential credential = new OAuth2Credential( "twitch", configuration.getCredentials().get("irc") ); Twitch4J ClientBuilder and OAuth2Credential
  21. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook ITwitchClient twitchClient = clientBuilder .withClientId(configuration.getApi().get("twitch_client_id")) .withClientSecret(configuration.getApi().get("twitch_client_secret" )) .withEnableHelix(true) .withChatAccount(credential) .withEnableChat(true) /* * Build the TwitchClient Instance */ .build(); Twitch4J Building the TwitchClient with Chat Enablement
  22. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook // Join a single channel twitchClient.getChat().joinChannel("PlayOverwatch"); // Connect to all channels for (String channel : configuration.getChannels()) { twitchClient.getChat().joinChannel(channel); } Twitch4J – Channels Join a Channel
  23. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook public class WriteChannelChatToConsole { /** * Register events of this class with the EventManager/EventHandler * * @param eventHandler SimpleEventHandler */ public WriteChannelChatToConsole(SimpleEventHandler eventHandler) { eventHandler.onEvent(ChannelMessageEvent.class, event -> onChannelMessage(event)); } Twitch4J – Channels Write Channel to Console
  24. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook /** * Subscribe to the ChannelMessage Event and write the output to the console */ public void onChannelMessage(ChannelMessageEvent event) { System.out.printf( "Channel [%s] - User[%s] - Message [%s]%n", event.getChannel().getName(), event.getUser().getName(), event.getMessage() ); } } Twitch4J – Channels Write Channel to Console (2)
  25. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook CreateClipList clipData = twitchClient.getHelix().createClip(accessToken, "149223493", false).execute(); clipData.getData().forEach(clip -> { System.out.println("Created Clip with ID: " + clip.getId()); }); Twitch4J – Clips Create a Clip
  26. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook ClipList clipList = twitchClient.getHelix().getClips(null, "488552", null, null, null, null, null, null).execute(); clipList.getData().forEach(clip -> { System.out.println("Found Clip: " + clip.getId()); }); Twitch4J – Clips Write Clip’s ID to the Console
  27. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook GameList resultList = twitchClient.getHelix().getGames(Arrays.asList(overwatchGameId), null).execute(); resultList.getGames().forEach(game -> { System.out.println("Game ID: " + game.getId() + " is " + game.getName()); }); Twitch4J – Games Write Game’s Name and ID to the Console
  28. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Demo Time @ivar_grimstad @wernerkeil
  29. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Java Enterprise Social Book I wrote a book “Enterprise Social for the Java Platform” (Apress, 2024) link.springer.com/book/10.1007/978-1-4842-9571-7 Examples on GitHub: github.com/Apress/enterprise-social-java-platform Mastodon Account: @[email protected]
  30. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Links & Ressourcen • github.com/Apress/enterprise-social-java-platform • https://jakarta.ee/ • Twitch: https://www.twitch.tv/ • Twitch Developers: https://dev.twitch.tv/ • Twitch4J: https://twitch4j.github.io/ • Twitch CLI: https://github.com/twitchdev/twitch-cli
  31. © 2022-2024 Werner Keil. Alle Rechte vorbehalten. #DWX24 #Twitch4J #EnterpriseSocial

    #Java jvm.social/@enterprisebook Thank You @wernerwedge