-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (100 loc) · 2.95 KB
/
main.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
// Copyright 2015 Sergey Serebryakov. All rights reserved.
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
)
type twitterCredentials struct {
ConsumerKey string `json:"key"`
ConsumerSecret string `json:"secret"`
}
const twitterAPIURL string = "https://api.twitter.com"
func makeAuthHandler(creds twitterCredentials) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
authHandler(creds, w, r)
}
}
func authHandler(creds twitterCredentials, w http.ResponseWriter, r *http.Request) {
url := twitterAPIURL + "/oauth2/token"
body := "grant_type=client_credentials"
bodyReader := bytes.NewBufferString(body)
req, parseErr := http.NewRequest("POST", url, bodyReader)
if parseErr != nil {
http.Error(w, parseErr.Error(), 500)
return
}
credsString := creds.ConsumerKey + ":" + creds.ConsumerSecret
var buffer bytes.Buffer
base64Encoder := base64.NewEncoder(base64.StdEncoding, &buffer)
base64Encoder.Write([]byte(credsString))
base64Encoder.Close()
encodedCreds := buffer.String()
authHeaderValue := "Basic " + encodedCreds
req.Header.Add("Authorization", authHeaderValue)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
doRequest(w, r, req)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
searchQuery := r.FormValue("query")
searchGeocode := r.FormValue("coords")
urlQuery :=
"q=" + url.QueryEscape(searchQuery) +
"&" + "geocode=" + searchGeocode +
"&" + "result_type=" + "recent"
url := &url.URL{
Scheme: "https",
Host: "api.twitter.com",
Path: "/1.1/search/tweets.json",
RawQuery: urlQuery,
}
log.Print(url.String())
req, parseErr := http.NewRequest("GET", url.String(), nil)
if parseErr != nil {
http.Error(w, parseErr.Error(), 500)
return
}
authToken := r.FormValue("authToken")
authHeaderValue := "Bearer " + authToken
req.Header.Add("Authorization", authHeaderValue)
doRequest(w, r, req)
}
func doRequest(w http.ResponseWriter, r *http.Request, reqToMake *http.Request) {
ctx := appengine.NewContext(r)
client := urlfetch.Client(ctx)
resp, httpErr := client.Do(reqToMake)
if httpErr != nil {
http.Error(w, httpErr.Error(), 500)
return
}
respBodyBuffer := new(bytes.Buffer)
respBodyBuffer.ReadFrom(resp.Body)
respBody := respBodyBuffer.String()
fmt.Fprint(w, respBody)
log.Print(respBody)
return
}
func readConfig() twitterCredentials {
configFile, openErr := os.Open("config/twitter_credentials.json")
if openErr != nil {
panic(openErr)
}
jsonParser := json.NewDecoder(configFile)
var creds twitterCredentials
if parseErr := jsonParser.Decode(&creds); parseErr != nil {
panic(parseErr)
}
return creds
}
func init() {
twitterCredentials := readConfig()
http.HandleFunc("/auth", makeAuthHandler(twitterCredentials))
http.HandleFunc("/search", searchHandler)
}