Skip to content

Instantly share code, notes, and snippets.

@icyflame
Last active December 15, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icyflame/7cbc4e70647619f01277542c248b319c to your computer and use it in GitHub Desktop.
Save icyflame/7cbc4e70647619f01277542c248b319c to your computer and use it in GitHub Desktop.
go-twitter library - app authentication doesn't fetch user timelines
package main
import (
"fmt"
"github.com/dghubble/go-twitter/twitter"
"golang.org/x/oauth2"
"os"
)
func main() {
config := &oauth2.Config{}
token := &oauth2.Token{AccessToken: os.Getenv("ACCESS_CODE")}
// 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, _, err := client.Users.Show(userShowParams)
fmt.Printf("USERS SHOW:\n%+v\n", user)
fmt.Printf("Error with Users show: %+v\n", err)
// users lookup
userLookupParams := &twitter.UserLookupParams{ScreenName: []string{"golang", "gophercon"}}
users, _, err := client.Users.Lookup(userLookupParams)
fmt.Printf("USERS LOOKUP:\n%+v\n", users)
fmt.Printf("Error with users lookup: %+v\n", err)
// status show
statusShowParams := &twitter.StatusShowParams{}
tweet, _, err := client.Statuses.Show(584077528026849280, statusShowParams)
fmt.Printf("STATUSES SHOW:\n%+v\n", tweet)
fmt.Printf("Error with status show: %+v\n", err)
// statuses lookup
statusLookupParams := &twitter.StatusLookupParams{ID: []int64{20}, TweetMode: "extended"}
tweets, _, err := client.Statuses.Lookup([]int64{573893817000140800}, statusLookupParams)
fmt.Printf("STATUSES LOOKUP:\n%+v\n", tweets)
fmt.Printf("Error with status lookup %+v\n", err)
// oEmbed status
statusOembedParams := &twitter.StatusOEmbedParams{ID: 691076766878691329, MaxWidth: 500}
oembed, _, err := client.Statuses.OEmbed(statusOembedParams)
fmt.Printf("OEMBED TWEET:\n%+v\n", oembed)
fmt.Printf("Error with Oembed tweet: %+v\n", err)
// user timeline
userTimelineParams := &twitter.UserTimelineParams{ScreenName: "_icyflame", Count: 2}
tweets, _, err = client.Timelines.UserTimeline(userTimelineParams)
fmt.Printf("USER TIMELINE:\n%+v\n", tweets)
fmt.Printf("Error with User timeline: %+v\n", err)
// search tweets
searchTweetParams := &twitter.SearchTweetParams{
Query: "happy birthday",
TweetMode: "extended",
Count: 3,
}
search, _, err := client.Search.Tweets(searchTweetParams)
fmt.Printf("SEARCH TWEETS:\n%+v\n", search)
fmt.Printf("SEARCH METADATA:\n%+v\n", search.Metadata)
fmt.Printf("Error with Search: %+v\n", err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment