forked from dghubble/oauth1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter-request.go
39 lines (32 loc) · 1.22 KB
/
twitter-request.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
// Twitter user-auth requests with an Access Token (token credential)
func main() {
// read credentials from environment variables
consumerKey := os.Getenv("TWITTER_CONSUMER_KEY")
consumerSecret := os.Getenv("TWITTER_CONSUMER_SECRET")
accessToken := os.Getenv("TWITTER_ACCESS_TOKEN")
accessSecret := os.Getenv("TWITTER_ACCESS_SECRET")
if consumerKey == "" || consumerSecret == "" || accessToken == "" || accessSecret == "" {
panic("Missing required environment variable")
}
config := oauth1.NewConfig(consumerKey, consumerSecret)
token := oauth1.NewToken(accessToken, accessSecret)
// httpClient will automatically authorize http.Request's
httpClient := config.Client(oauth1.NoContext, token)
path := "https://api.twitter.com/1.1/statuses/home_timeline.json?count=2"
resp, _ := httpClient.Get(path)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Response Body:\n%v\n", string(body))
// Nicer: Pass OAuth1 client to go-twitter API
api := twitter.NewClient(httpClient)
tweets, _, _ := api.Timelines.HomeTimeline(nil)
fmt.Printf("User's HOME TIMELINE:\n%+v\n", tweets)
}