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