项目作者: kelr

项目描述 :
Twitch Helix API and PubSub Library
高级语言: Go
项目地址: git://github.com/kelr/gundyr.git
创建时间: 2020-01-25T00:07:48Z
项目社区:https://github.com/kelr/gundyr

开源协议:GNU General Public License v3.0

下载


gundyr

GoDoc Go Report Card

Gundyr provides an easy to use interface to the Helix Twitch API and Twitch PubSub.

It handles both app access as well as user access tokens. All tokens used are automatically refreshed.

This is a work in progress and not all Helix or PubSub endpoints are supported yet.

Install

  1. $ go get github.com/kelr/gundyr

Docs

Documentation can be found at godoc. Examples can be found in the examples directory.

Usage

Helix - Getting a User ID

  1. cfg := &gundyr.HelixConfig{
  2. ClientID: clientID,
  3. ClientSecret: clientSecret,
  4. }
  5. c, err := gundyr.NewHelix(cfg)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. userID, err := c.UserToID("kyrotobi")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. log.Println(userID)

Helix - Using User Access Tokens

If an OAuth2 token is not provided to HelixConfig, authentication will attempt to use the OAuth2 Client Credentials flow to obtain a App Access token.

  1. // See examples/auth_token.go on creating/retrieving tokens.
  2. cfg := &gundyr.HelixConfig{
  3. ClientID: clientID,
  4. ClientSecret: clientSecret,
  5. Scopes: []string{"user:read:email"},
  6. RedirectURI: redirectURI,
  7. Token: token,
  8. }
  9. c, err := gundyr.NewHelix(cfg)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. email, err := c.GetUserEmail("your-username")
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. log.Println(email)

PubSub - Subscribing to Channel Point Redemptions

  1. func handleChannelPoints(event *pubsub.ChannelPointsEvent) {
  2. fmt.Println(event.Redemption.Reward.Title)
  3. }
  4. func main() {
  5. scopes := []string{"channel:read:redemptions"}
  6. // Setup OAuth2 configuration
  7. config, err := auth.NewUserAuth(clientID, clientSecret, redirectURI, &scopes)
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. // See examples/auth_token.go for an example on creating a new token.
  12. token, err := auth.RetrieveTokenFile(config, tokenFile)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. // Create a PubSub client and listen to the topics.
  17. client := pubsub.NewClient(userID, token)
  18. client.ListenChannelPoints(handleChannelPoints)
  19. client.Connect()
  20. select {}
  21. }

Contributions

Any and all contributions or bug fixes are appreciated.