-
Notifications
You must be signed in to change notification settings - Fork 3
/
twigo.go
91 lines (79 loc) · 1.8 KB
/
twigo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package twigo
import (
"strings"
"github.com/arshamalh/twigo/utils"
"github.com/mrjones/oauth"
)
type Config struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessSecret string
BearerToken string
}
func NewClient(config *Config) (*Client, error) {
keys_exists := config.ConsumerKey != "" && config.ConsumerSecret != "" && config.AccessToken != "" && config.AccessSecret == ""
if !keys_exists {
if config.BearerToken == "" {
if bearer_token, err := utils.BearerFinder(config.ConsumerKey, config.ConsumerSecret); err == nil {
config.BearerToken = bearer_token
} else {
return nil, err
}
}
userID := ""
if config.AccessToken != "" {
userID = strings.Split(config.AccessToken, "-")[0]
}
return &Client{
nil,
"",
"",
"",
"",
config.BearerToken,
true,
userID,
OAuth_2,
}, nil
}
userID := strings.Split(config.AccessToken, "-")[0]
// TODO: I'm authenticating here, but Do I need to authenticate every once in a while?
consumer := oauth.NewConsumer(
config.ConsumerKey,
config.ConsumerSecret,
oauth.ServiceProvider{
RequestTokenUrl: "https://api.twitter.com/oauth/request_token",
AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
AccessTokenUrl: "https://api.twitter.com/oauth/access_token",
})
t := oauth.AccessToken{
Token: config.AccessToken,
Secret: config.AccessSecret,
}
authorizedClient, err := consumer.MakeHttpClient(&t)
return &Client{
authorizedClient,
config.ConsumerKey,
config.ConsumerSecret,
config.AccessToken,
config.AccessSecret,
config.BearerToken,
false,
userID,
OAuth_Default,
}, err
}
func NewBearerOnlyClient(bearerToken string) (*Client, error) {
return &Client{
nil,
"",
"",
"",
"",
bearerToken,
true,
"",
OAuth_2,
}, nil
}