You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.1 KiB

package main
import (
"fmt"
"log"
"os"
"github.com/dghubble/oauth1"
twauth "github.com/dghubble/oauth1/twitter"
)
const outOfBand = "oob"
var config oauth1.Config
// main performs Twitter PIN-based 3-legged OAuth 1 from the command line
func main() {
// read credentials from environment variables
consumerKey := os.Getenv("TWITTER_CONSUMER_KEY")
consumerSecret := os.Getenv("TWITTER_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
log.Fatal("Required environment variable missing.")
}
config = oauth1.Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
CallbackURL: outOfBand,
Endpoint: twauth.AuthorizeEndpoint,
}
requestToken, err := login()
if err != nil {
log.Fatalf("Request Token Phase: %s", err.Error())
}
accessToken, err := receivePIN(requestToken)
if err != nil {
log.Fatalf("Access Token Phase: %s", err.Error())
}
fmt.Println("Consumer was granted an access token to act on behalf of a user.")
fmt.Printf("token: %s\nsecret: %s\n", accessToken.Token, accessToken.TokenSecret)
}
func login() (requestToken string, err error) {
requestToken, _, err = config.RequestToken()
if err != nil {
return "", err
}
authorizationURL, err := config.AuthorizationURL(requestToken)
if err != nil {
return "", err
}
fmt.Printf("Open this URL in your browser:\n%s\n", authorizationURL.String())
return requestToken, err
}
func receivePIN(requestToken string) (*oauth1.Token, error) {
fmt.Printf("Paste your PIN here: ")
var verifier string
_, err := fmt.Scanf("%s", &verifier)
if err != nil {
return nil, err
}
// Twitter ignores the oauth_signature on the access token request. The user
// to which the request (temporary) token corresponds is already known on the
// server. The request for a request token earlier was validated signed by
// the consumer. Consumer applications can avoid keeping request token state
// between authorization granting and callback handling.
accessToken, accessSecret, err := config.AccessToken(requestToken, "secret does not matter", verifier)
if err != nil {
return nil, err
}
return oauth1.NewToken(accessToken, accessSecret), err
}