A simple Go package for Login with Twitter
go get github.com/pandodao/twitter-login-go
- Sign in to Twitter Developer Dashboard, create a new app.
- And then you can get the
TwitterApiKey
andTwitterApiSecret
of the app.
twitterClient := twitter.New(TwitterApiKey, TwitterApiSecret)
requestUrl, err := twitterClient.GetAuthURL(callbackURL)
if err != nil {
return err
}
// ask user to visit the requestUrl
Twitter will ask the user to confirm the login, and then redirect user to the callbackURL
with a oauth_token
and oauth_verifier
parameter.
callbackURL?oauth_token=xxx&oauth_verifier=xxx
You must read the oauth_token
and oauth_verifier
parameters from the URL, and then use it to get the access token.
oauthToken := r.URL.Query().Get("oauth_token")
oauthVerifier := r.URL.Query().Get("oauth_verifier")
accessToken, err := twitterClient.GetAccessToken(oauthToken, oauthVerifier)
if err != nil {
return err
}
user, err := s.twitterClient.Verify()
if err != nil {
return err
}
// save `user` and `accessToken` for later use