-
Notifications
You must be signed in to change notification settings - Fork 184
/
app.go
104 lines (91 loc) · 2.63 KB
/
app.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
package controllers
import (
"encoding/json"
"io/ioutil"
"github.com/mrjones/oauth"
"github.com/revel/examples/twitter-oauth/app/models"
"github.com/revel/revel"
)
var TWITTER = oauth.NewConsumer(
"VgRjky4dTA1U2Ck16MmZw",
"l8lOLyIF3peCFEvrEoTc8h4oFwieAFgPM6eeTRo30I",
oauth.ServiceProvider{
AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
RequestTokenUrl: "https://api.twitter.com/oauth/request_token",
AccessTokenUrl: "https://api.twitter.com/oauth/access_token",
},
)
type Application struct {
*revel.Controller
}
func (c Application) Index() revel.Result {
user := getUser()
if user.AccessToken == nil {
return c.Render()
}
// We have a token, so look for mentions.
resp, err := TWITTER.Get(
"https://api.twitter.com/1.1/statuses/mentions_timeline.json",
map[string]string{"count": "10"},
user.AccessToken)
if err != nil {
c.Log.Error("mentions error", "error", err)
return c.Render()
}
defer resp.Body.Close()
// Extract the mention text.
mentions := []struct {
Text string `json:text`
}{}
err = json.NewDecoder(resp.Body).Decode(&mentions)
if err != nil {
c.Log.Error("JSON error", "error", err)
}
c.Log.Info("Success", "mentions", mentions)
return c.Render(mentions)
}
func (c Application) SetStatus(status string) revel.Result {
resp, err := TWITTER.PostForm(
"http://api.twitter.com/1.1/statuses/update.json",
map[string]string{"status": status},
getUser().AccessToken,
)
if err != nil {
c.Log.Error(err.Error())
return c.RenderError(err)
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
c.Log.Info(string(bodyBytes))
c.Response.ContentType = "application/json"
return c.RenderText(string(bodyBytes))
}
// Twitter authentication
func (c Application) Authenticate(oauth_verifier string) revel.Result {
user := getUser()
if oauth_verifier != "" {
// We got the verifier; now get the access token, store it and back to index
accessToken, err := TWITTER.AuthorizeToken(user.RequestToken, oauth_verifier)
if err == nil {
user.AccessToken = accessToken
} else {
c.Log.Error("Error connecting to twitter:", "error", err)
}
return c.Redirect(Application.Index)
}
requestToken, url, err := TWITTER.GetRequestTokenAndUrl("http://127.0.0.1:9000/Application/Authenticate")
if err == nil {
// We received the unauthorized tokens in the OAuth object - store it before we proceed
user.RequestToken = requestToken
return c.Redirect(url)
} else {
c.Log.Error("Error connecting to twitter:", "error", err)
}
return c.Redirect(Application.Index)
}
func getUser() *models.User {
return models.FindOrCreate("guest")
}
func init() {
TWITTER.Debug(true)
}