-
Notifications
You must be signed in to change notification settings - Fork 4
/
oauth.go
73 lines (59 loc) · 2.05 KB
/
oauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"github.com/mrjones/oauth"
)
const (
twAuthURL = "https://api.twitter.com/oauth/authorize"
twTokenURL = "https://api.twitter.com/oauth/request_token"
twAccessTokenURL = "https://api.twitter.com/oauth/access_token"
)
// Oauth struct for holding a consumer
type Oauth struct {
Consumer *oauth.Consumer
}
// AuthenticationRequest holds our request for later confirmation
type AuthenticationRequest struct {
RequestToken *oauth.RequestToken
URL string
}
func newOauth(ConsumerKey, ConsumerSecret string) Oauth {
c := oauth.NewConsumer(
ConsumerKey,
ConsumerSecret,
oauth.ServiceProvider{
RequestTokenUrl: twTokenURL,
AuthorizeTokenUrl: twAuthURL,
AccessTokenUrl: twAccessTokenURL,
})
return Oauth{c}
}
func (o Oauth) newAuthenticationRequest() (*AuthenticationRequest, error) {
requestToken, url, err := o.Consumer.GetRequestTokenAndUrl("oob")
if err != nil {
return nil, err
}
return &AuthenticationRequest{requestToken, url}, nil
}
func (o Oauth) getAccessToken(RequestToken *oauth.RequestToken, code string) (*oauth.AccessToken, error) {
accessToken, err := o.Consumer.AuthorizeToken(RequestToken, code)
return accessToken, err
}
// AuthWithTwitter call with the consumer key and secret to start
// an OOB/PIN authentication with Twitter
func AuthWithTwitter(consumerKey, consumerSecret string) {
oauth := newOauth(consumerKey, consumerSecret)
ar, _ := oauth.newAuthenticationRequest()
fmt.Printf("In your browser, log in to your twitter account. Then visit: \n%s\n", ar.URL)
fmt.Println("After logged in, you will be promoted with a pin number")
fmt.Println("Enter the pin number here:")
pinCode := ""
fmt.Scanln(&pinCode)
accessToken, err := oauth.getAccessToken(ar.RequestToken, pinCode)
if err != nil {
fmt.Printf("Error getting your access token: %s\n", err)
return
}
fmt.Println("Success! The following are your access token and secret. Update config.toml with these keys")
fmt.Printf("TokenKey = \"%s\"\nTokenSecret = \"%s\"\n", accessToken.Token, accessToken.Secret)
}