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.

37 lines
1009 B

  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. Consumer_key string `json:"consumer_key"`
  12. Consumer_secret string `json:"consumer_secret"`
  13. Access_token_key string `json:"access_token_key"`
  14. Access_token_secret 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. configu := oauth1.NewConfig(config.Consumer_key, config.Consumer_secret)
  26. token := oauth1.NewToken(config.Access_token_key, config.Access_token_secret)
  27. httpClient := configu.Client(oauth1.NoContext, token)
  28. // twitter client
  29. client = twitter.NewClient(httpClient)
  30. return client
  31. }