-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added new endpoint * corrected typo * added the unit tests * added example * updated the readme for the example * linting and naming
- Loading branch information
1 parent
235d0d4
commit a0d8485
Showing
8 changed files
with
519 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
v2/_examples/tweets/timeline/user-tweet-reverse-chronological-timeline/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
twitter "github.com/g8rswimmer/go-twitter/v2" | ||
) | ||
|
||
type authorize struct { | ||
Token string | ||
} | ||
|
||
func (a authorize) Add(req *http.Request) { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token)) | ||
} | ||
|
||
/** | ||
In order to run, the user will need to provide the bearer token and the list of tweet ids. | ||
**/ | ||
func main() { | ||
token := flag.String("token", "", "twitter API token") | ||
userID := flag.String("user_id", "", "user id") | ||
flag.Parse() | ||
|
||
client := &twitter.Client{ | ||
Authorizer: authorize{ | ||
Token: *token, | ||
}, | ||
Client: http.DefaultClient, | ||
Host: "https://api.twitter.com", | ||
} | ||
opts := twitter.UserTweetReverseChronologicalTimelineOpts{ | ||
TweetFields: []twitter.TweetField{twitter.TweetFieldCreatedAt, twitter.TweetFieldAuthorID, twitter.TweetFieldConversationID, twitter.TweetFieldPublicMetrics, twitter.TweetFieldContextAnnotations}, | ||
UserFields: []twitter.UserField{twitter.UserFieldUserName}, | ||
Expansions: []twitter.Expansion{twitter.ExpansionAuthorID}, | ||
MaxResults: 5, | ||
} | ||
|
||
fmt.Println("Callout to tweet user reverse chronological timeline callout") | ||
|
||
timeline, err := client.UserTweetReverseChronologicalTimeline(context.Background(), *userID, opts) | ||
if err != nil { | ||
log.Panicf("user reverse chronological timeline error: %v", err) | ||
} | ||
|
||
dictionaries := timeline.Raw.TweetDictionaries() | ||
|
||
enc, err := json.MarshalIndent(dictionaries, "", " ") | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
fmt.Println(string(enc)) | ||
|
||
metaBytes, err := json.MarshalIndent(timeline.Meta, "", " ") | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
fmt.Println(string(metaBytes)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.