mirror of
https://github.com/taigrr/wtf
synced 2026-03-23 11:42:17 -07:00
Support both bearer + consumer tokens for Twitter modules
* Add support to both the twitter and twitterstats modules for authenticating using both bearer tokens as well as consumer key + secret. * A bearer token is defaulted to if it's supplied * Add this support to both the twitterstats module as well as to the existing twitter module, modifying its functionality to re-use the same HTTP client and handle authentication upfront via oauth2
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package twitterstats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
screenNames []string
|
||||
bearerToken string
|
||||
}
|
||||
|
||||
// TwitterStats Represents a stats snapshot for a single Twitter user at a point in time
|
||||
@@ -37,12 +39,23 @@ func NewClient(settings *Settings) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
conf := &clientcredentials.Config{
|
||||
ClientID: settings.consumerKey,
|
||||
ClientSecret: settings.consumerSecret,
|
||||
TokenURL: "https://api.twitter.com/oauth2/token",
|
||||
var httpClient *http.Client
|
||||
// If a bearer token is supplied, use that directly. Otherwise, let the Oauth client fetch a token
|
||||
// using the consumer key and secret.
|
||||
if settings.bearerToken == "" {
|
||||
conf := &clientcredentials.Config{
|
||||
ClientID: settings.consumerKey,
|
||||
ClientSecret: settings.consumerSecret,
|
||||
TokenURL: "https://api.twitter.com/oauth2/token",
|
||||
}
|
||||
httpClient = conf.Client(oauth2.NoContext)
|
||||
} else {
|
||||
ctx := context.Background()
|
||||
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{
|
||||
AccessToken: settings.bearerToken,
|
||||
TokenType: "Bearer",
|
||||
}))
|
||||
}
|
||||
httpClient := conf.Client(oauth2.NoContext)
|
||||
|
||||
client := Client{
|
||||
httpClient: httpClient,
|
||||
|
||||
Reference in New Issue
Block a user