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.

43 lines
1.2 KiB

7 years ago
  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. //TwitterConfig stores the data from json twitterConfig.json file
  10. type TwitterConfig struct {
  11. ScreenName string `json:"screenName"`
  12. ConsumerKey string `json:"consumer_key"`
  13. ConsumerSecret string `json:"consumer_secret"`
  14. AccessToken string `json:"access_token"`
  15. AccessTokenSecret string `json:"access_token_secret"`
  16. }
  17. var twitterConfig TwitterConfig
  18. func readTwitterConfigTokensAndConnect() (client *twitter.Client) {
  19. file, e := ioutil.ReadFile("twitterConfig.json")
  20. if e != nil {
  21. fmt.Println("error:", e)
  22. }
  23. content := string(file)
  24. json.Unmarshal([]byte(content), &twitterConfig)
  25. fmt.Println("twitterConfig.json read comlete")
  26. fmt.Print("connecting to twitter api --> ")
  27. configu := oauth1.NewConfig(twitterConfig.ConsumerKey, twitterConfig.ConsumerSecret)
  28. token := oauth1.NewToken(twitterConfig.AccessToken, twitterConfig.AccessTokenSecret)
  29. httpClient := configu.Client(oauth1.NoContext, token)
  30. // twitter client
  31. client = twitter.NewClient(httpClient)
  32. fmt.Println("connection successful")
  33. return client
  34. }