/
tokens.go
102 lines (85 loc) · 2.2 KB
/
tokens.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
package streamingtwitter
import (
"encoding/json"
"fmt"
"github.com/garyburd/go-oauth/oauth"
"io/ioutil"
"net/http"
"os"
)
var (
// File permissions for the token file.
tokenFilePermission = os.FileMode(0600)
)
type ClientTokens struct {
// Location to our token storage file (JSON format)
TokenFile string `json:"-"`
// Token for the actual application
App *oauth.Credentials
// Token for the user of the application
User *oauth.Credentials
}
type ClientTokensError struct {
Msg string
}
func (e ClientTokensError) Error() string {
return e.Msg
}
// You get a token for your App from Twitter. Put this within the App section
// of the JSON token file. The user's token will be requested, then written
// and saved to this file.
func (t *ClientTokens) Token(oc *oauth.Client) (*oauth.Credentials, error) {
if t.TokenFile == "" {
return nil, &ClientTokensError{
Msg: "no token file supplied",
}
}
cf, err := ioutil.ReadFile(t.TokenFile)
if err != nil {
return nil, err
}
if err := json.Unmarshal(cf, t); err != nil {
return nil, err
}
if t.App == nil {
return nil, &ClientTokensError{
Msg: "missing \"App\" token",
}
}
if t.App.Token == "" || t.App.Secret == "" {
return nil, &ClientTokensError{
Msg: "missing app's Token or Secret",
}
}
oc.Credentials = *t.App
var token *oauth.Credentials
if t.User == nil {
token = &oauth.Credentials{}
} else {
token = t.User
}
if token.Token == "" || token.Secret == "" {
tempCredentials, err := oc.RequestTemporaryCredentials(http.DefaultClient, "oob", nil)
if err != nil {
return nil, err
}
url := oc.AuthorizationURL(tempCredentials, nil)
fmt.Fprintf(os.Stdout, "Before we can continue ...\nGo to:\n\n\t%s\n\nAuthorize the application and enter in the verification code: ", url)
var authCode string
fmt.Scanln(&authCode)
token, _, err = oc.RequestToken(http.DefaultClient, tempCredentials, authCode)
if err != nil {
return nil, err
}
// Save the user token within our token file
t.User = token
save, err := json.Marshal(t)
if err != nil {
return nil, err
}
if err := ioutil.WriteFile(t.TokenFile, save, tokenFilePermission); err != nil {
return nil, err
}
}
return token, nil
}