Skip to content

Commit

Permalink
Adding Twitter oauth pin helper
Browse files Browse the repository at this point in the history
  • Loading branch information
araddon committed Dec 22, 2013
1 parent 30e2f92 commit 56eaf2d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/twitter_auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


This is an example that uses Twitter oauth PIN

Get your Twitter Token, and Key from https://dev.twitter.com/apps




```sh

go clean && go build

./twitter_auth --ck=MY_APP_CONSUMER_KEY --cs=MY_APP_SECRET


# it will give you a url to go to browser


```
67 changes: 67 additions & 0 deletions examples/twitter_auth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"flag"
"fmt"
oauth "github.com/araddon/goauth"
u "github.com/araddon/gou"
"io/ioutil"
)

/*
Usage:
go clean && go run
./twitter_auth --ck=MY_APP_CONSUMER_KEY --cs=MY_APP_SECRET
*/
var (
ck *string = flag.String("ck", "", "Consumer Key")
cs *string = flag.String("cs", "", "Consumer Secret")
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.Printf("\n\n\ttoken=%s secret=%s \n\n\n", at.Token, at.Secret)
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 {
userData, _ := ioutil.ReadAll(r.Body)
if len(userData) > 0 {
jh := u.NewJsonHelper(userData)
fmt.Println(string(jh.PrettyJson()))
}
} else {
fmt.Println(r)
fmt.Println("Twitter verify failed")
}

}

0 comments on commit 56eaf2d

Please sign in to comment.