You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "github.com/dghubble/go-twitter/twitter"
  7. "github.com/dghubble/oauth1"
  8. )
  9. //Config stores the data from json twitterConfig.json file
  10. type Config struct {
  11. ConsumerKey string `json:"consumer_key"`
  12. ConsumerSecret string `json:"consumer_secret"`
  13. AccessTokenKey string `json:"access_token_key"`
  14. AccessTokenSecret string `json:"access_token_secret"`
  15. }
  16. func readConfigTokensAndConnect() (client *twitter.Client) {
  17. var config Config
  18. file, e := ioutil.ReadFile("twitterConfig.json")
  19. if e != nil {
  20. fmt.Println("error:", e)
  21. }
  22. content := string(file)
  23. json.Unmarshal([]byte(content), &config)
  24. fmt.Println("twitterConfig.json read comlete")
  25. fmt.Print("connecting to twitter api --> ")
  26. configu := oauth1.NewConfig(config.ConsumerKey, config.ConsumerSecret)
  27. token := oauth1.NewToken(config.AccessTokenKey, config.AccessTokenSecret)
  28. httpClient := configu.Client(oauth1.NoContext, token)
  29. // twitter client
  30. client = twitter.NewClient(httpClient)
  31. fmt.Println("connection successful")
  32. return client
  33. }