-
Notifications
You must be signed in to change notification settings - Fork 13
/
server_client.go
63 lines (51 loc) · 1.41 KB
/
server_client.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
package twitter
import (
"log"
"github.com/mrjones/oauth"
)
func NewServerClient(consumerKey, consumerSecret string) *ServerClient {
//newClient := NewClient(consumerKey, consumerKey)
newServer := new(ServerClient)
newServer.OAuthConsumer = oauth.NewConsumer(
consumerKey,
consumerSecret,
oauth.ServiceProvider{
RequestTokenUrl: OAUTH_REQUES_TOKEN,
AuthorizeTokenUrl: OAUTH_AUTH_TOKEN,
AccessTokenUrl: OAUTH_ACCESS_TOKEN,
},
)
//Enable debug info
newServer.OAuthConsumer.Debug(false)
// newServer.Client = *newClient
newServer.OAuthTokens = make(map[string]*oauth.RequestToken)
return newServer
}
type ServerClient struct {
Client
OAuthConsumer *oauth.Consumer
OAuthTokens map[string]*oauth.RequestToken
}
func (s *ServerClient) GetAuthURL(tokenUrl string) (string, error) {
token, requestUrl, err := s.OAuthConsumer.GetRequestTokenAndUrl(tokenUrl)
if err != nil {
log.Println(err)
return "", nil
}
// Make sure to save the token, we'll need it for AuthorizeToken()
s.OAuthTokens[token.Token] = token
return requestUrl, nil
}
func (s *ServerClient) CompleteAuth(tokenKey, verificationCode string) error {
accessToken, err := s.OAuthConsumer.AuthorizeToken(s.OAuthTokens[tokenKey], verificationCode)
if err != nil {
log.Println(err)
return err
}
s.HttpConn, err = s.OAuthConsumer.MakeHttpClient(accessToken)
if err != nil {
log.Println(err)
return err
}
return nil
}