forked from dghubble/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-auth.go
71 lines (58 loc) · 2.2 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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/coreos/pkg/flagutil"
"github.com/dghubble/go-twitter/twitter"
"golang.org/x/oauth2"
)
func main() {
flags := flag.NewFlagSet("app-auth", flag.ExitOnError)
accessToken := flags.String("app-access-token", "", "Twitter Application Access Token")
flags.Parse(os.Args[1:])
flagutil.SetFlagsFromEnv(flags, "TWITTER")
if *accessToken == "" {
log.Fatal("Application Access Token required")
}
config := &oauth2.Config{}
token := &oauth2.Token{AccessToken: *accessToken}
// OAuth2 http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext, token)
// 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)
}