Skip to content

Instantly share code, notes, and snippets.

@araddon
Created July 2, 2013 23:46
Show Gist options
  • Save araddon/5914272 to your computer and use it in GitHub Desktop.
Save araddon/5914272 to your computer and use it in GitHub Desktop.
Twitter Oauth Test
package main
import (
"flag"
"fmt"
oauth "github.com/akrennmair/goauth"
"io/ioutil"
)
/*
Usage:
go run twoauth.go --ck=MY_APP_CONSUMER_KEY --cs=MY_APP_SECRET
*/
var (
user *string = flag.String("user", "", "twitter username")
ck *string = flag.String("ck", "", "Consumer Key")
cs *string = flag.String("cs", "", "Consumer Secret")
ot *string = flag.String("ot", "", "Oauth Token")
osec *string = flag.String("os", "", "OAuthTokenSecret")
goauthcon *oauth.OAuthConsumer
)
func main() {
flag.Parse()
goauthcon = &oauth.OAuthConsumer{
Service: "twitter",
RequestTokenURL: "https://api.twitter.com/oauth/request_token",
AccessTokenURL: "https://api.twitter.com/oauth/access_token",
AuthorizationURL: "https://api.twitter.com/oauth/authorize",
ConsumerKey: *ck,
ConsumerSecret: *cs,
CallBackURL: "oob",
}
s, rt, err := goauthcon.GetRequestAuthorizationURL()
if err != nil {
fmt.Println(err)
return
}
var pin string
fmt.Printf("Open %s In your browser.\n Allow access and then enter the PIN number\n", s)
fmt.Printf("PIN Number: ")
fmt.Scanln(&pin)
at := goauthcon.GetAccessToken(rt.Token, pin)
fmt.Println(at.Token)
r, err := goauthcon.Get("https://api.twitter.com/1.1/account/verify_credentials.json", nil, at)
if err != nil {
fmt.Println(err)
return
}
if r != nil && r.Body != nil && r.StatusCode == 200 {
data, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(data))
} else {
fmt.Println(r)
fmt.Println("Twitter verify failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment