forked from dghubble/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-auth.go
80 lines (66 loc) · 2.52 KB
/
app-auth.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
package main
import (
"flag"
"fmt"
"log"
"github.com/coreos/pkg/flagutil"
"github.com/dghubble/go-twitter/twitter"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
func main() {
flags := struct {
consumerKey string
consumerSecret string
}{}
flag.StringVar(&flags.consumerKey, "consumer-key", "", "Twitter Consumer Key")
flag.StringVar(&flags.consumerSecret, "consumer-secret", "", "Twitter Consumer Secret")
flag.Parse()
flagutil.SetFlagsFromEnv(flag.CommandLine, "TWITTER")
if flags.consumerKey == "" || flags.consumerSecret == "" {
log.Fatal("Application Access Token required")
}
// oauth2 configures a client that uses app credentials to keep a fresh token
config := &clientcredentials.Config{
ClientID: flags.consumerKey,
ClientSecret: flags.consumerSecret,
TokenURL: "https://api.twitter.com/oauth2/token",
}
// http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext)
// Twitter client
client := twitter.NewClient(httpClient)
// user show
userShowParams := &twitter.UserShowParams{ScreenName: "golang"}
user, _, _ := client.Users.Show(userShowParams)
fmt.Printf("USERS SHOW:\n%+v\n", user)
// users lookup
userLookupParams := &twitter.UserLookupParams{ScreenName: []string{"golang", "gophercon"}}
users, _, _ := client.Users.Lookup(userLookupParams)
fmt.Printf("USERS LOOKUP:\n%+v\n", users)
// status show
statusShowParams := &twitter.StatusShowParams{}
tweet, _, _ := client.Statuses.Show(584077528026849280, statusShowParams)
fmt.Printf("STATUSES SHOW:\n%+v\n", tweet)
// statuses lookup
statusLookupParams := &twitter.StatusLookupParams{ID: []int64{20}, TweetMode: "extended"}
tweets, _, _ := client.Statuses.Lookup([]int64{573893817000140800}, statusLookupParams)
fmt.Printf("STATUSES LOOKUP:\n%+v\n", tweets)
// oEmbed status
statusOembedParams := &twitter.StatusOEmbedParams{ID: 691076766878691329, MaxWidth: 500}
oembed, _, _ := client.Statuses.OEmbed(statusOembedParams)
fmt.Printf("OEMBED TWEET:\n%+v\n", oembed)
// user timeline
userTimelineParams := &twitter.UserTimelineParams{ScreenName: "golang", Count: 2}
tweets, _, _ = client.Timelines.UserTimeline(userTimelineParams)
fmt.Printf("USER TIMELINE:\n%+v\n", tweets)
// search tweets
searchTweetParams := &twitter.SearchTweetParams{
Query: "happy birthday",
TweetMode: "extended",
Count: 3,
}
search, _, _ := client.Search.Tweets(searchTweetParams)
fmt.Printf("SEARCH TWEETS:\n%+v\n", search)
fmt.Printf("SEARCH METADATA:\n%+v\n", search.Metadata)
}