romerocm

day02-07-example1

Apr 19th, 2022 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.23 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     // other imports
  5.     "fmt"
  6.     "log"
  7.     "os"
  8.  
  9.     "github.com/dghubble/go-twitter/twitter"
  10.     "github.com/dghubble/oauth1"
  11. )
  12.  
  13. // Credentials stores all of our access/consumer tokens
  14. // and secret keys needed for authentication against
  15. // the twitter REST API.
  16. type Credentials struct {
  17.     ConsumerKey       string
  18.     ConsumerSecret    string
  19.     AccessToken       string
  20.     AccessTokenSecret string
  21. }
  22.  
  23. // getClient is a helper function that will return a twitter client
  24. // that we can subsequently use to send tweets, or to stream new tweets
  25. // this will take in a pointer to a Credential struct which will contain
  26. // everything needed to authenticate and return a pointer to a twitter Client
  27. // or an error
  28. func getClient(creds *Credentials) (*twitter.Client, error) {
  29.     // Pass in your consumer key (API Key) and your Consumer Secret (API Secret)
  30.     config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
  31.     // Pass in your Access Token and your Access Token Secret
  32.     token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
  33.  
  34.     httpClient := config.Client(oauth1.NoContext, token)
  35.     client := twitter.NewClient(httpClient)
  36.  
  37.     // Verify Credentials
  38.     verifyParams := &twitter.AccountVerifyParams{
  39.         SkipStatus:   twitter.Bool(true),
  40.         IncludeEmail: twitter.Bool(true),
  41.     }
  42.  
  43.     // we can retrieve the user and verify if the credentials
  44.     // we have used successfully allow us to log in!
  45.     user, _, err := client.Accounts.VerifyCredentials(verifyParams)
  46.     if err != nil {
  47.         return nil, err
  48.     }
  49.  
  50.     log.Printf("User's ACCOUNT:\n%+v\n", user)
  51.     return client, nil
  52. }
  53. func main() {
  54.     fmt.Println("Go-Twitter Bot v0.01")
  55.     creds := Credentials{
  56.         AccessToken:       os.Getenv("ACCESS_TOKEN"),
  57.         AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
  58.         ConsumerKey:       os.Getenv("CONSUMER_KEY"),
  59.         ConsumerSecret:    os.Getenv("CONSUMER_SECRET"),
  60.     }
  61.  
  62.     client, err := getClient(&creds)
  63.     if err != nil {
  64.         log.Println("Error getting Twitter Client")
  65.         log.Println(err)
  66.     }
  67.  
  68.     tweet, resp, err := client.Statuses.Update("A Test Tweet from the future, testing a #DevOpsTraineeProgram Program that tweets, tweet tweet", nil)
  69.     if err != nil {
  70.         log.Println(err)
  71.     }
  72.     log.Printf("%+v\n", resp)
  73.     log.Printf("%+v\n", tweet)
  74. }
  75.  
Add Comment
Please, Sign In to add comment