-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappauth.go
More file actions
80 lines (70 loc) · 2.12 KB
/
appauth.go
File metadata and controls
80 lines (70 loc) · 2.12 KB
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
package kuruvi
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
)
type appAuth struct {
*Auth
bearerToken string
}
func newAppAuth(key, secret string) *appAuth {
return &appAuth{Auth: &Auth{ConsumerKey: key, ConsumerSecret: secret}}
}
func (a *appAuth) getBearerToken() {
client := &http.Client{}
//Step 1: Encode consumer key and secret
encodedKeySecret := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s",
url.QueryEscape(a.ConsumerKey),
url.QueryEscape(a.ConsumerSecret))))
//Step 2: Obtain a bearer token
//The body of the request must be grant_type=client_credentials
reqBody := bytes.NewBuffer([]byte(`grant_type=client_credentials`))
//The request must be a HTTP POST request
req, err := http.NewRequest("POST", "https://api.twitter.com/oauth2/token", reqBody)
if err != nil {
log.Fatal(err, client, req)
}
//The request must include an Authorization header formatted as
//Basic <base64 encoded value from step 1>.
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", encodedKeySecret))
//The request must include a Content-Type header with
//the value of application/x-www-form-urlencoded;charset=UTF-8.
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
req.Header.Add("Content-Length", strconv.Itoa(reqBody.Len()))
//Issue the request and get the bearer token from the JSON you get back
resp, err := client.Do(req)
if err != nil {
log.Fatal(err, resp)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err, respBody)
}
type BearerToken struct {
AccessToken string `json:"access_token"`
}
var b BearerToken
json.Unmarshal(respBody, &b)
a.bearerToken = b.AccessToken
}
func (a *appAuth) setAuthHeader(r *http.Request) {
if a.bearerToken == "" {
a.getBearerToken()
}
//Step 3: Authenticate API requests with the bearer token
//include an Authorization header formatted as
//Bearer <bearer token value from step 2>
r.Header.Add("Authorization",
fmt.Sprintf("Bearer %s", a.bearerToken))
}
func (a *appAuth) getAuthType() int {
return App
}