-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
48 lines (39 loc) · 1.12 KB
/
request.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
package main
import (
"bytes"
"fmt"
"net/http"
"strconv"
)
// Request retreives the bearer token using the consumer key and secret
func Request(consumerKey string, consumerSecret string, apiURL string) ([]byte, error) {
// Required Header and Body according to Twitter API documentation
reqBody := bytes.NewBuffer([]byte(`grant_type=client_credentials`))
req, err := http.NewRequest("POST", apiURL, reqBody)
encodedKeySecret := encodedKeySecret(consumerKey, consumerSecret)
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", encodedKeySecret))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
req.Header.Add("Content-Length", strconv.Itoa(reqBody.Len()))
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ParseBody(resp)
if err != nil {
return nil, err
}
return data, err
}
func ParseBody(resp *http.Response) ([]byte, error) {
var buffer bytes.Buffer
_, err := buffer.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}