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.0 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. type Config struct {
  10. Consumer_key string `json:"consumer_key"`
  11. Consumer_secret string `json:"consumer_secret"`
  12. Access_token_key string `json:"access_token_key"`
  13. Access_token_secret string `json:"access_token_secret"`
  14. }
  15. func readConfigTokensAndConnect() (client *twitter.Client) {
  16. var config Config
  17. file, e := ioutil.ReadFile("twitterConfig.json")
  18. if e != nil {
  19. fmt.Println("error:", e)
  20. }
  21. content := string(file)
  22. json.Unmarshal([]byte(content), &config)
  23. //fmt.Printf("%+v\n", config)
  24. fmt.Println("twitterConfig.json read comlete")
  25. fmt.Print("connecting to twitter api --> ")
  26. configu := oauth1.NewConfig(config.Consumer_key, config.Consumer_secret)
  27. token := oauth1.NewToken(config.Access_token_key, config.Access_token_secret)
  28. httpClient := configu.Client(oauth1.NoContext, token)
  29. // twitter client
  30. client = twitter.NewClient(httpClient)
  31. fmt.Println("connection successfull")
  32. return client
  33. }