diff --git a/v2/_examples/tweets/README.md b/v2/_examples/tweets/README.md index ed63e11..a6d3e94 100644 --- a/v2/_examples/tweets/README.md +++ b/v2/_examples/tweets/README.md @@ -47,6 +47,8 @@ The examples can be run my providing some options, including the authorization t * [Users who have liked a Tweet](./likes/user-likes-lookup) * [Tweets liked by an user](./likes/tweet-likes-lookup) +* [Allows a user ID to like a Tweet](./likes/user-like-tweet) +* [Allows a user ID to unlike a Tweet](./likes/user-unlike-tweet) ### [Hide Replies](https://developer.twitter.com/en/docs/twitter-api/tweets/hide-replies/introduction) diff --git a/v2/_examples/tweets/likes/user-like-tweet/main.go b/v2/_examples/tweets/likes/user-like-tweet/main.go new file mode 100644 index 0000000..4e01c7b --- /dev/null +++ b/v2/_examples/tweets/likes/user-like-tweet/main.go @@ -0,0 +1,52 @@ +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 user ids. +**/ +func main() { + token := flag.String("token", "", "twitter API token") + userID := flag.String("user_id", "", "user id") + tweetID := flag.String("tweet_id", "", "tweet id") + flag.Parse() + + client := &twitter.Client{ + Authorizer: authorize{ + Token: *token, + }, + Client: http.DefaultClient, + Host: "https://api.twitter.com", + } + + fmt.Println("Callout to user like tweet callout") + + userResponse, err := client.UserLikes(context.Background(), *userID, *tweetID) + if err != nil { + log.Panicf("user like tweet error: %v", err) + } + + enc, err := json.MarshalIndent(userResponse, "", " ") + if err != nil { + log.Panic(err) + } + fmt.Println(string(enc)) + +} diff --git a/v2/_examples/tweets/likes/user-unlike-tweet/main.go b/v2/_examples/tweets/likes/user-unlike-tweet/main.go new file mode 100644 index 0000000..aebc372 --- /dev/null +++ b/v2/_examples/tweets/likes/user-unlike-tweet/main.go @@ -0,0 +1,52 @@ +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 user ids. +**/ +func main() { + token := flag.String("token", "", "twitter API token") + userID := flag.String("user_id", "", "user id") + tweetID := flag.String("tweet_id", "", "tweet id") + flag.Parse() + + client := &twitter.Client{ + Authorizer: authorize{ + Token: *token, + }, + Client: http.DefaultClient, + Host: "https://api.twitter.com", + } + + fmt.Println("Callout to user unlike tweet callout") + + userResponse, err := client.DeleteUserLikes(context.Background(), *userID, *tweetID) + if err != nil { + log.Panicf("user unlike tweet error: %v", err) + } + + enc, err := json.MarshalIndent(userResponse, "", " ") + if err != nil { + log.Panic(err) + } + fmt.Println(string(enc)) + +}