Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the examples for managing likes #118

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions v2/_examples/tweets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
52 changes: 52 additions & 0 deletions v2/_examples/tweets/likes/user-like-tweet/main.go
Original file line number Diff line number Diff line change
@@ -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))

}
52 changes: 52 additions & 0 deletions v2/_examples/tweets/likes/user-unlike-tweet/main.go
Original file line number Diff line number Diff line change
@@ -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))

}