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.

39 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.Println("twitterConfig.json read comlete")
  24. fmt.Print("connecting to twitter api --> ")
  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. fmt.Println("connection successfull")
  31. return client
  32. }