forked from dghubble/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-auth.go
70 lines (59 loc) · 2.19 KB
/
user-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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/coreos/pkg/flagutil"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
func main() {
flags := flag.NewFlagSet("user-auth", flag.ExitOnError)
consumerKey := flags.String("consumer-key", "", "Twitter Consumer Key")
consumerSecret := flags.String("consumer-secret", "", "Twitter Consumer Secret")
accessToken := flags.String("access-token", "", "Twitter Access Token")
accessSecret := flags.String("access-secret", "", "Twitter Access Secret")
flags.Parse(os.Args[1:])
flagutil.SetFlagsFromEnv(flags, "TWITTER")
if *consumerKey == "" || *consumerSecret == "" || *accessToken == "" || *accessSecret == "" {
log.Fatal("Consumer key/secret and Access token/secret required")
}
config := oauth1.NewConfig(*consumerKey, *consumerSecret)
token := oauth1.NewToken(*accessToken, *accessSecret)
// OAuth1 http.Client will automatically authorize Requests
httpClient := config.Client(oauth1.NoContext, token)
// Twitter client
client := twitter.NewClient(httpClient)
// Verify Credentials
verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
}
user, _, _ := client.Accounts.VerifyCredentials(verifyParams)
fmt.Printf("User's ACCOUNT:\n%+v\n", user)
// Home Timeline
homeTimelineParams := &twitter.HomeTimelineParams{
Count: 2,
TweetMode: "extended",
}
tweets, _, _ := client.Timelines.HomeTimeline(homeTimelineParams)
fmt.Printf("User's HOME TIMELINE:\n%+v\n", tweets)
// Mention Timeline
mentionTimelineParams := &twitter.MentionTimelineParams{
Count: 2,
TweetMode: "extended",
}
tweets, _, _ = client.Timelines.MentionTimeline(mentionTimelineParams)
fmt.Printf("User's MENTION TIMELINE:\n%+v\n", tweets)
// Retweets of Me Timeline
retweetTimelineParams := &twitter.RetweetsOfMeTimelineParams{
Count: 2,
TweetMode: "extended",
}
tweets, _, _ = client.Timelines.RetweetsOfMeTimeline(retweetTimelineParams)
fmt.Printf("User's 'RETWEETS OF ME' TIMELINE:\n%+v\n", tweets)
// Update (POST!) Tweet (uncomment to run)
// tweet, _, _ := client.Statuses.Update("just setting up my twttr", nil)
// fmt.Printf("Posted Tweet\n%v\n", tweet)
}