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

Twitter oauth2 #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ To do this, you will need to setup the following environment variables:
- GITHUB_ACCESS_TOKEN
- TWITTER_CONSUMER_KEY
- TWITTER_CONSUMER_SECRET
- TWITTER_ACCESS_TOKEN
- TWITTER_ACCESS_SECRET

If you want the content to be publish in a README file on a repo, you also need these variables
- GITHUB_PUBLISH_REPO_OWNER (Your Github username)
Expand Down Expand Up @@ -82,7 +80,7 @@ _NOTE: The key is used in the --provider or --pr option_

| Name | Key | Environment Variables | Observation |
|--------------|:-------:|----------------------| -------------|
| Twitter | twitter | TWITTER_CONSUMER_KEY<br>TWITTER_CONSUMER_SECRET<br>TWITTER_ACCESS_TOKEN<br>TWITTER_ACCESS_SECRET | |
| Twitter | twitter | TWITTER_CONSUMER_KEY<br>TWITTER_CONSUMER_SECRET | |
| Github | github | GITHUB_PUBLISH_REPO_OWNER<br>GITHUB_PUBLISH_REPO_NAME<br>GITHUB_PUBLISH_REPO_FILE | For now it is only going to be posted in the README file and the repository must be **public** |

_NOTE: The key is used in the --publisher or --pub option_
Expand Down
4 changes: 0 additions & 4 deletions cmd/larry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ var (

twitterConsumerKey = envString("TWITTER_CONSUMER_KEY", "")
twitterConsumerSecret = envString("TWITTER_CONSUMER_SECRET", "")
twitterAccessToken = envString("TWITTER_ACCESS_TOKEN", "")
twitterAccessSecret = envString("TWITTER_ACCESS_SECRET", "")
)

func main() {
Expand Down Expand Up @@ -113,8 +111,6 @@ func getPublishers(cfg config.Config) (map[string]larry.Publisher, error) {
accessKeys := twitter.AccessKeys{
TwitterConsumerKey: twitterConsumerKey,
TwitterConsumerSecret: twitterConsumerSecret,
TwitterAccessToken: twitterAccessToken,
TwitterAccessSecret: twitterAccessSecret,
}
pubs[v] = twitter.NewPublisher(accessKeys, cfg)
} else if v == publisher.Github {
Expand Down
14 changes: 8 additions & 6 deletions publisher/twitter/publisher.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package twitter

import (
"context"
"fmt"
"log"
"strings"

"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
"github.com/ezeoleaf/larry/config"
"github.com/ezeoleaf/larry/domain"
"golang.org/x/oauth2/clientcredentials"
)

// Publisher represents the publisher client
Expand All @@ -21,18 +22,19 @@ type Publisher struct {
type AccessKeys struct {
TwitterConsumerKey string
TwitterConsumerSecret string
TwitterAccessToken string
TwitterAccessSecret string
}

// NewPublisher returns a new publisher
func NewPublisher(accessKeys AccessKeys, cfg config.Config) Publisher {
log.Print("New Twitter Publisher")

oauthCfg := oauth1.NewConfig(accessKeys.TwitterConsumerKey, accessKeys.TwitterConsumerSecret)
oauthToken := oauth1.NewToken(accessKeys.TwitterAccessToken, accessKeys.TwitterAccessSecret)
config := &clientcredentials.Config{
ClientID: accessKeys.TwitterConsumerKey,
ClientSecret: accessKeys.TwitterConsumerSecret,
TokenURL: "https://api.twitter.com/oauth2/token",
}

client := twitter.NewClient(oauthCfg.Client(oauth1.NoContext, oauthToken))
client := twitter.NewClient(config.Client(context.Background()))

p := Publisher{
Config: cfg,
Expand Down