-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication_only.go
More file actions
88 lines (78 loc) · 2.47 KB
/
application_only.go
File metadata and controls
88 lines (78 loc) · 2.47 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
81
82
83
84
85
86
87
88
package main
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
)
const (
ConsumerKey = "<YOUR CONSUMER KEY HERE>"
ConsumerSecret = "<YOUR CONSUMER SECRET HERE>"
)
func main() {
client := &http.Client{}
//Step 1: Encode consumer key and secret
encodedKeySecret := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s",
url.QueryEscape(ConsumerKey),
url.QueryEscape(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)
//choose your API endpoint that supports application only auth context
//and create a request object with that
twitterEndPoint := "https://api.twitter.com/1.1/application/rate_limit_status.json"
req, err = http.NewRequest("GET", twitterEndPoint, nil)
if err != nil {
log.Fatal(err)
}
//Step 3: Authenticate API requests with the bearer token
//include an Authorization header formatted as
//Bearer <bearer token value from step 2>
req.Header.Add("Authorization",
fmt.Sprintf("Bearer %s", b.AccessToken))
//Issue the request and get the JSON API response
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)
}
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
fmt.Println(string(respBody))
}