Skip to content

Commit

Permalink
- update example to include twitterv2
Browse files Browse the repository at this point in the history
- fix twitterv2 endpointProfile request
- fix twitterv2 crashing when casting data
  • Loading branch information
IRelaxxx committed Aug 4, 2022
1 parent 6fc8acb commit 617b035
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
8 changes: 8 additions & 0 deletions examples/main.go
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/markbates/goth/providers/tiktok"
"github.com/markbates/goth/providers/twitch"
"github.com/markbates/goth/providers/twitter"
"github.com/markbates/goth/providers/twitterv2"
"github.com/markbates/goth/providers/typetalk"
"github.com/markbates/goth/providers/uber"
"github.com/markbates/goth/providers/vk"
Expand All @@ -73,6 +74,12 @@ import (

func main() {
goth.UseProviders(
// Use twitterv2 instead of twitter if you only have access to the Essential API Level
// the twitter provider uses a v1.1 API that is not available to the Essential Level
twitterv2.New(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), "http://localhost:3000/auth/twitterv2/callback"),
// If you'd like to use authenticate instead of authorize in TwitterV2 provider, use this instead.
// twitterv2.NewAuthenticate(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), "http://localhost:3000/auth/twitterv2/callback"),

twitter.New(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), "http://localhost:3000/auth/twitter/callback"),
// If you'd like to use authenticate instead of authorize in Twitter provider, use this instead.
// twitter.NewAuthenticate(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), "http://localhost:3000/auth/twitter/callback"),
Expand Down Expand Up @@ -191,6 +198,7 @@ func main() {
m["battlenet"] = "Battlenet"
m["paypal"] = "Paypal"
m["twitter"] = "Twitter"
m["twitterv2"] = "Twitter"
m["salesforce"] = "Salesforce"
m["typetalk"] = "Typetalk"
m["slack"] = "Slack"
Expand Down
13 changes: 6 additions & 7 deletions providers/twitterv2/twitterv2.go
Expand Up @@ -108,7 +108,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {

response, err := p.consumer.Get(
endpointProfile,
nil,
map[string]string{"user.fields": "id,name,username,description,profile_image_url,location"},
sess.AccessToken)
if err != nil {
return user, err
Expand Down Expand Up @@ -136,13 +136,12 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
user.RawData = userInfo.Data
user.Name = user.RawData["name"].(string)
user.NickName = user.RawData["username"].(string)
if user.RawData["email"] != nil {
user.Email = user.RawData["email"].(string)
}
user.Description = user.RawData["description"].(string)
user.AvatarURL = user.RawData["profile_image_url"].(string)
user.UserID = user.RawData["id"].(string)
user.Location = user.RawData["location"].(string)
if user.RawData["location"] != nil {
user.Location = user.RawData["location"].(string)
}
user.AccessToken = sess.AccessToken.Token
user.AccessTokenSecret = sess.AccessToken.Secret
return user, err
Expand All @@ -162,12 +161,12 @@ func newConsumer(provider *Provider, authURL string) *oauth.Consumer {
return c
}

//RefreshToken refresh token is not provided by twitter
// RefreshToken refresh token is not provided by twitter
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
return nil, errors.New("Refresh token is not provided by twitter")
}

//RefreshTokenAvailable refresh token is not provided by twitter
// RefreshTokenAvailable refresh token is not provided by twitter
func (p *Provider) RefreshTokenAvailable() bool {
return false
}
2 changes: 1 addition & 1 deletion providers/twitterv2/twitterv2_test.go
Expand Up @@ -69,7 +69,7 @@ func Test_FetchUser(t *testing.T) {
a.Equal("1234", user.UserID)
a.Equal("Springfield", user.Location)
a.Equal("TOKEN", user.AccessToken)
a.Equal("duffman@springfield.com", user.Email)
a.Equal("", user.Email)
}

func Test_SessionFromJSON(t *testing.T) {
Expand Down

0 comments on commit 617b035

Please sign in to comment.