/
OAuth.go
96 lines (79 loc) · 1.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"errors"
"github.com/mrjones/oauth"
"io/ioutil"
"os"
)
// Fanfou's OAuth authention urls
var fanfouService = &oauth.ServiceProvider{
RequestTokenUrl: "http://fanfou.com/oauth/request_token",
AuthorizeTokenUrl: "http://fanfou.com/oauth/authorize",
AccessTokenUrl: "http://fanfou.com/oauth/access_token",
}
// A struct to store Fanfou's OAuth information
type Fanfou struct {
Key string
Secret string
AccessToken *oauth.AccessToken
}
// Fanfou Client struct
type FanfouClient struct {
*oauth.Consumer
Token *oauth.AccessToken
}
// Check if we have our access token already
func (c *Fanfou) CheckAccessToken() bool {
if c.AccessToken == nil {
return false
}
// We have access token already
return true
}
// Load config from a json file
func (c *Fanfou) LoadConfig(name string) error {
jsonBytes, err := ioutil.ReadFile(name)
if err != nil {
return err
}
err = json.Unmarshal(jsonBytes, c)
if err != nil {
return err
}
return nil
}
// Write config to a json file
func (c *Fanfou) WriteConfig(name string) error {
jsonBytes, err := json.Marshal(c)
if err != nil {
return err
}
err = ioutil.WriteFile(name, jsonBytes, os.ModePerm)
if err != nil {
return err
}
return nil
}
// Create a new Fanfou client, root should be RootFanfou or RootSandbox
func (c *Fanfou) NewFanfouClient() (*FanfouClient, error) {
if c.Key == "" || c.Secret == "" {
return nil, errors.New("No consumer key or secret")
}
return &FanfouClient{
oauth.NewConsumer(c.Key, c.Secret, *fanfouService),
c.AccessToken,
}, nil
}
// A wrapper for oauth.Consumer.AuthorizeToken. Please remember to store your
// access token to Fanfou's OAuth information struct after call this function.
func (c *FanfouClient) AuthorizeToken(
rtoken *oauth.RequestToken,
verifyCode string) (*oauth.AccessToken, error) {
atoken, err := c.Consumer.AuthorizeToken(rtoken, verifyCode)
if err == nil {
// store access token
c.Token = atoken
}
return atoken, err
}