Skip to content

pandodao/twitter-login-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

twitter-login-go

A simple Go package for Login with Twitter

Installation

go get github.com/pandodao/twitter-login-go

Preparation

Usage

1. Init client

twitterClient := twitter.New(TwitterApiKey, TwitterApiSecret)

2. Generate the Auth URL

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.

3. Exchange code for 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