Skip to content

Commit

Permalink
Merge pull request #9 from arshamalh/dev
Browse files Browse the repository at this point in the history
Twitter Error for wrong authentication added.
  • Loading branch information
arshamalh authored Jun 22, 2022
2 parents f8b4a2d + 1b3f452 commit 05866f8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import "github.com/arshamalh/twigo"

twigo.NewClient((&twigo.Config{
ConsumerKey: "ConsumerKey",
ConsumerSecret: "ConsumerSecret",
AccessToken: "AccessToken",
AccessSecret: "AccessTokenSecret",
ConsumerSecret: "ConsumerSecret",
AccessToken: "AccessToken",
AccessSecret: "AccessTokenSecret",
// Both of "bearer token" or "four other keys (ConsumerKey, ...)" is not mandatory.
// We'll find bearer token automatically if it's not specified.
// Also, you can use twigo.utils.BearerFinder() function.
BearerToken: "BearerToken",
BearerToken: "BearerToken",
}))
```

Expand Down
13 changes: 12 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/arshamalh/twigo/utils"
)
Expand Down Expand Up @@ -71,7 +72,14 @@ func (c *Client) get_request(route string, oauth_type OAuthType, params Map, end

request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
client := http.Client{}
return client.Do(request)
resp, err := client.Do(request)
if resp.StatusCode != 200 {
err := SpecialError{}
json.NewDecoder(resp.Body).Decode(&err)
defer resp.Body.Close()
return nil, err.Error()
}
return resp, err
}
}

Expand Down Expand Up @@ -1118,6 +1126,7 @@ func (c *Client) GetUserByUsername(username string, params Map) (*UserResponse,
endpoint_parameters := []string{
"expansions", "tweet.fields", "user.fields",
}
username = strings.Replace(username, "@", "", 1)
route := fmt.Sprintf("users/by/username/%s", username)
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
Expand Down Expand Up @@ -1154,6 +1163,8 @@ func (c *Client) GetUsersByIDs(user_ids []string, params Map) (*UsersResponse, e
// Returns a variety of information about one or more users specified by
// the requested usernames.
//
// usernames should not have @ at the beginning
//
// https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by
func (c *Client) GetUsersByUsernames(usernames []string, params Map) (*UsersResponse, error) {
endpoint_parameters := []string{
Expand Down
12 changes: 12 additions & 0 deletions entities.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package twigo

import (
"fmt"
"net/http"
"strconv"
"time"
Expand All @@ -22,6 +23,17 @@ type ErrorEntity struct {
Message string `json:"message"`
}

type SpecialError struct {
Title string `json:"title"`
Type string `json:"type"`
Status int `json:"status"`
Detail string `json:"detail"`
}

func (e *SpecialError) Error() error {
return fmt.Errorf("%s - %d - %s - %s", e.Title, e.Status, e.Type, e.Detail)
}

type IncludesEntity struct {
Users []entities.User `json:"users"`
Tweets []entities.Tweet `json:"tweets"`
Expand Down
23 changes: 9 additions & 14 deletions twigo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package twigo

import (
"fmt"
"strings"

"github.com/arshamalh/twigo/utils"
Expand All @@ -21,7 +20,11 @@ func NewClient(config *Config) (*Client, error) {

if !keys_exists {
if config.BearerToken == "" {
return nil, fmt.Errorf("consumer key, consumer secret, access token and access token secret must be provided")
if bearer_token, err := utils.BearerFinder(config.ConsumerKey, config.ConsumerSecret); err == nil {
config.BearerToken = bearer_token
} else {
return nil, err
}
}

userID := ""
Expand All @@ -31,25 +34,17 @@ func NewClient(config *Config) (*Client, error) {

return &Client{
nil,
config.ConsumerKey,
config.ConsumerSecret,
config.AccessToken,
config.AccessSecret,
"",
"",
"",
"",
config.BearerToken,
true,
userID,
OAuth_2,
}, nil
}

if config.BearerToken == "" {
if bearer_token, err := utils.BearerFinder(config.ConsumerKey, config.ConsumerSecret); err == nil {
config.BearerToken = bearer_token
} else {
return nil, err
}
}

userID := strings.Split(config.AccessToken, "-")[0]

// TODO: I'm authenticating here, but Do I need to authenticate every once in a while?
Expand Down

0 comments on commit 05866f8

Please sign in to comment.