This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
/
auth.go
172 lines (153 loc) · 4.09 KB
/
auth.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
package twitterscraper
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
)
const (
loginURL = "https://api.twitter.com/1.1/onboarding/task.json"
bearerToken2 = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
)
type flow struct {
Errors []struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"errors"`
FlowToken string `json:"flow_token"`
Status string `json:"status"`
}
func (s *Scraper) getFlowToken(data map[string]interface{}) (string, error) {
headers := http.Header{
"Authorization": []string{"Bearer " + s.bearerToken},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"Mozilla/5.0 (Linux; Android 11; Nokia G20) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.88 Mobile Safari/537.36"},
"X-Guest-Token": []string{s.guestToken},
"X-Twitter-Auth-Type": []string{"OAuth2Client"},
"X-Twitter-Active-User": []string{"yes"},
"X-Twitter-Client-Language": []string{"en"},
}
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", loginURL, bytes.NewReader(jsonData))
if err != nil {
return "", err
}
req.Header = headers
resp, err := s.client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var info flow
err = json.NewDecoder(resp.Body).Decode(&info)
if err != nil {
return "", err
}
if len(info.Errors) > 0 {
return "", fmt.Errorf("auth error (%d): %v", info.Errors[0].Code, info.Errors[0].Message)
}
return info.FlowToken, nil
}
// IsLoggedIn check if scraper logged in
func (s *Scraper) IsLoggedIn() bool {
return s.isLogged
}
// Login to Twitter
func (s *Scraper) Login(username string, password string) error {
s.setBearerToken(bearerToken2)
err := s.GetGuestToken()
if err != nil {
return err
}
// flow start
data := map[string]interface{}{
"flow_name": "login",
"input_flow_data": map[string]interface{}{
"flow_context": map[string]interface{}{
"debug_overrides": map[string]interface{}{},
"start_location": map[string]interface{}{"location": "splash_screen"},
},
},
}
flowToken, err := s.getFlowToken(data)
if err != nil {
return err
}
// flow instrumentation step
data = map[string]interface{}{
"flow_token": flowToken,
"subtask_inputs": []map[string]interface{}{
{
"subtask_id": "LoginJsInstrumentationSubtask",
"js_instrumentation": map[string]interface{}{"response": "{}", "link": "next_link"},
},
},
}
flowToken, err = s.getFlowToken(data)
if err != nil {
return err
}
// flow username step
data = map[string]interface{}{
"flow_token": flowToken,
"subtask_inputs": []map[string]interface{}{
{
"subtask_id": "LoginEnterUserIdentifierSSO",
"settings_list": map[string]interface{}{
"setting_responses": []map[string]interface{}{
{
"key": "user_identifier",
"response_data": map[string]interface{}{"text_data": map[string]interface{}{"result": username}},
},
},
"link": "next_link",
},
},
},
}
flowToken, err = s.getFlowToken(data)
if err != nil {
return err
}
// flow password step
data = map[string]interface{}{
"flow_token": flowToken,
"subtask_inputs": []map[string]interface{}{
{
"subtask_id": "LoginEnterPassword",
"enter_password": map[string]interface{}{"password": password, "link": "next_link"},
},
},
}
flowToken, err = s.getFlowToken(data)
if err != nil {
return err
}
// flow duplication check
data = map[string]interface{}{
"flow_token": flowToken,
"subtask_inputs": []map[string]interface{}{
{
"subtask_id": "AccountDuplicationCheck",
"check_logged_in_account": map[string]interface{}{"link": "AccountDuplicationCheck_false"},
},
},
}
_, err = s.getFlowToken(data)
if err != nil {
return err
}
s.isLogged = true
return nil
}
// Logout is reset session
func (s *Scraper) Logout() {
s.isLogged = false
s.guestToken = ""
s.client.Jar, _ = cookiejar.New(nil)
s.setBearerToken(bearerToken)
}