arnaucube
arnaucube/argosPublic Self-hosted repository
argos/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	Consumer_key        string `json:"consumer_key"`
15	Consumer_secret     string `json:"consumer_secret"`
16	Access_token_key    string `json:"access_token_key"`
17	Access_token_secret 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.Consumer_key, config.Consumer_secret)
32	token := oauth1.NewToken(config.Access_token_key, config.Access_token_secret)
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}