-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.go
180 lines (150 loc) · 4.13 KB
/
token.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package igtoken
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
)
const (
authUrl string = "https://api.instagram.com/oauth/authorize/"
loginUrl string = "https://www.instagram.com"
scopeSep string = "+"
BASIC Scope = "basic"
PUBLIC_CONTENT Scope = "public_content"
FOLLOWER_LIST Scope = "follower_list"
COMMENTS Scope = "comments"
RELATIONSHIPS Scope = "relationships"
LIKES Scope = "likes"
)
type Scope string
type TokenClient struct {
clientId string
redirectUrl string
userNick string
userPassword string
client *http.Client
}
func NewClient(clientId, redirectUrl, userNick, userPassword string) (tc *TokenClient) {
jar, _ := cookiejar.New(nil)
return &TokenClient{
clientId: clientId,
redirectUrl: redirectUrl,
userNick: userNick,
userPassword: userPassword,
client: &http.Client{
Jar: jar,
},
}
}
func (t *TokenClient) GetToken(scopes ...Scope) (accessToken string, err error) {
// Create server to receive token
var u *url.URL
var port string
var closeServer func() bool
u, err = url.Parse(t.redirectUrl)
if _, port, err = net.SplitHostPort(u.Host); err != nil {
return
}
if closeServer, err = startServer(port, u.Path); err != nil {
return
}
defer closeServer()
// Get login page
var r *http.Response
var req *http.Request
if req, err = http.NewRequest("GET", authUrl, new(bytes.Buffer)); err != nil {
return
}
values := req.URL.Query()
values.Add("client_id", t.clientId)
values.Add("redirect_uri", t.redirectUrl)
values.Add("response_type", "token")
if len(scopes) > 0 {
values.Add("scope", joinScopes(scopes))
}
req.URL.RawQuery = values.Encode()
r, err = t.client.Do(req)
// Do login
var csrfToken, action string
if csrfToken, action, err = parseForm(r.Body); err != nil {
return
}
if r, err = t.login(csrfToken, action); err != nil {
return
}
// If we have already authorized this app we will get token straight away
f := r.Request.URL.Fragment
if len(f) > 13 && f[0:13] == "access_token=" {
accessToken = f[13:]
return
}
// We did not get token from redirect url.
// This could mean couple of things:
// - We got error message in json format
// - App was not authorized so we got auth form
// - We could some other error message
var b []byte
if b, err = ioutil.ReadAll(r.Body); err != nil {
return
}
// We got empty body but no access token so something is wrong
if len(b) == 0 {
err = fmt.Errorf("Empty body with status code %d", r.StatusCode)
}
// Looks like we got error message in json format
if b[0] == '{' {
err = fmt.Errorf("%s", b)
return
}
// Authorize app for account
if csrfToken, action, err = parseForm(ioutil.NopCloser(bytes.NewReader(b))); err != nil {
return
}
if r, err = t.auth(csrfToken, action); err != nil {
return
}
f = r.Request.URL.Fragment
if len(f) > 13 && f[0:13] == "access_token=" {
accessToken = f[13:]
return
}
alert := parseAlert(ioutil.NopCloser(bytes.NewReader(b)))
if alert != "" {
err = fmt.Errorf("%s", alert)
return
}
err = fmt.Errorf("%s", b)
return
}
func (t *TokenClient) login(csrftToken, action string) (r *http.Response, err error) {
formData := url.Values{}
formData.Add("csrfmiddlewaretoken", csrftToken)
formData.Add("username", t.userNick)
formData.Add("password", t.userPassword)
var req *http.Request
if req, err = http.NewRequest("POST", loginUrl+action, bytes.NewBufferString(formData.Encode())); err != nil {
return
}
req.Header.Add("Referer", loginUrl+action)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if r, err = t.client.Do(req); err != nil {
return
}
return
}
func (t *TokenClient) auth(csrftToken, action string) (r *http.Response, err error) {
formData := url.Values{}
formData.Add("csrfmiddlewaretoken", csrftToken)
formData.Add("allow", "Authorize")
var req *http.Request
if req, err = http.NewRequest("POST", loginUrl+action, bytes.NewBufferString(formData.Encode())); err != nil {
return
}
req.Header.Add("Referer", loginUrl+action)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r, err = t.client.Do(req)
return
}