-
Notifications
You must be signed in to change notification settings - Fork 15
/
weibo.go
317 lines (282 loc) · 7.94 KB
/
weibo.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Author: Tony.Shao
* Email: [email protected]
* Github: github.com/xiocode
* File: api.go
* Description: weibo api proxy
*/
package weigo
import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
simplejson "github.com/bitly/go-simplejson"
log "github.com/golang/glog"
to "github.com/gosexy/to"
)
const (
HTTP_GET int = 0
HTTP_POST int = 1
HTTP_UPLOAD int = 2
)
func call(client *http.Client, the_url string, method int, authorization string, params map[string]interface{}) ([]byte, error) {
var url_params string
var multipart_data *bytes.Buffer //For Upload Image
var http_url string
var http_body io.Reader
var content_type string
var request *http.Request
var HTTP_METHOD string
var err error
switch method {
case HTTP_GET:
HTTP_METHOD = "GET"
url_params, err = encodeParams(params)
http_url = fmt.Sprintf("%v?%v", the_url, url_params)
http_body = nil
case HTTP_POST:
HTTP_METHOD = "POST"
url_params, err = encodeParams(params)
content_type = "application/x-www-form-urlencoded"
http_url = the_url
http_body = strings.NewReader(url_params)
case HTTP_UPLOAD:
HTTP_METHOD = "POST"
the_url = strings.Replace(the_url, "https://api.", "https://upload.api.", 1)
content_type, multipart_data, err = encodeMultipart(params)
http_url = the_url
http_body = multipart_data
}
if err != nil {
return nil, err
}
request, err = http.NewRequest(HTTP_METHOD, http_url, http_body)
if err != nil {
return nil, err
}
request.Header.Add("Accept-Encoding", "gzip")
switch method {
case HTTP_POST:
request.Header.Add("Content-Type", content_type)
case HTTP_UPLOAD:
request.Header.Add("Content-Type", content_type)
request.Header.Add("Content-Length", to.String(multipart_data.Len()))
}
if authorization != "" {
request.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", authorization))
}
response, err := client.Do(request) // Do Request
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := read_body(response)
if err != nil {
return nil, err
}
return body, nil
}
func encodeParams(params map[string]interface{}) (string, error) {
if len(params) > 0 {
values := url.Values{}
for key, value := range params {
values.Add(key, to.String(value))
}
return values.Encode(), nil
}
return "", errors.New("Params Is Empty!")
}
func encodeMultipart(params map[string]interface{}) (multipartContentType string, multipartData *bytes.Buffer, err error) {
if len(params) > 0 {
multipartData := new(bytes.Buffer)
bufferWriter := multipart.NewWriter(multipartData) // type *bytes.Buffer
defer bufferWriter.Close()
var multipartContentType string
for key, value := range params {
switch value.(type) {
case *os.File:
picdata, err := bufferWriter.CreateFormFile(key, value.(*os.File).Name())
if err != nil {
return "", nil, err
}
multipartContentType = bufferWriter.FormDataContentType()
io.Copy(picdata, value.(*os.File))
default:
bufferWriter.WriteField(key, to.String(value))
}
}
return multipartContentType, multipartData, nil
}
return "", nil, errors.New("Params Is Empty!")
}
func read_body(response *http.Response) ([]byte, error) {
switch response.Header.Get("Content-Encoding") {
case "gzip":
reader, err := gzip.NewReader(response.Body)
if err != nil {
return nil, err
}
defer reader.Close()
contents, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return contents, nil
default:
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return contents, nil
}
return nil, errors.New("Unknow Errors")
}
type APIClient struct {
app_key string
app_secret string
redirect_uri string
response_type string
domain string
auth_url string
api_url string
version string
access_token string
expires int64
Pool *Pool
}
func (a *APIClient) call(base_url, uri, extension, access_token string, method int, params map[string]interface{}, result interface{}) error {
client, err := a.Pool.Get()
if err != nil {
log.Errorln(err)
return err
}
defer a.Pool.Put(client)
url := fmt.Sprintf("%s%s%s", base_url, uri, extension)
body, err := call(client.(*http.Client), url, method, access_token, params)
if err != nil {
log.Errorln(err)
return err
}
if len(body) == 0 {
return errors.New("Nothing Return From Http Requests!")
}
jsonbody, err := simplejson.NewJson(body)
if err != nil {
log.Errorln(err)
return err
}
_, ok := jsonbody.CheckGet("error_code")
if ok {
errcode, _ := jsonbody.Get("error_code").Int64()
errmessage, _ := jsonbody.Get("error").String()
err := &APIError{When: time.Now(), ErrorCode: errcode, Message: errmessage}
return err
}
if json.Unmarshal(body, result); err != nil {
log.Errorln(err)
return err
}
return nil
}
func (api *APIClient) is_expires() bool {
return api.access_token == "" || api.expires < time.Now().Unix()
}
func NewAPIClient(app_key, app_secret, redirect_uri, response_type string) *APIClient {
http_pool, err := NewConnPool(5, 10, func() (interface{}, error) {
return &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
deadline := time.Now().Add(5 * time.Second)
conn, err := net.DialTimeout(network, addr, 5*time.Second)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline)
return conn, nil
},
ResponseHeaderTimeout: 5 * time.Second,
// Proxy: http.ProxyURL(proxy),
},
}, nil
})
if err != nil {
return nil
}
return &APIClient{
app_key: app_key,
app_secret: app_secret,
redirect_uri: redirect_uri,
response_type: response_type,
domain: "api.weibo.com",
version: "2",
Pool: http_pool,
auth_url: fmt.Sprintf("https://%s/oauth2/", "api.weibo.com"),
api_url: fmt.Sprintf("https://%s/%s/", "api.weibo.com", "2"),
}
}
func (api *APIClient) SetAccessToken(access_token string, expires int64) *APIClient {
api.access_token = access_token
api.expires = expires
return api
}
func (api *APIClient) GetAuthorizeUrl(params map[string]interface{}) (string, error) {
url_params := map[string]interface{}{
"client_id": api.app_key,
"response_type": api.response_type,
"redirect_uri": api.redirect_uri,
}
for key, value := range params {
url_params[key] = value
}
encode_params, err := encodeParams(url_params)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s?%s", api.auth_url, "authorize", encode_params), nil
}
func (api *APIClient) RequestAccessToken(code string, result interface{}) error {
api.SetAccessToken("", 0)
return api.Auth("access_token",
map[string]interface{}{
"client_id": api.app_key,
"client_secret": api.app_secret,
"redirect_uri": api.redirect_uri,
"code": code,
"grant_type": "authorization_code",
},
result)
}
func (a *APIClient) GET(uri string, params map[string]interface{}, result interface{}) error {
return a.call(a.api_url, uri, ".json", a.access_token, HTTP_GET, params, result)
}
func (a *APIClient) POST(uri string, params map[string]interface{}, result interface{}) error {
return a.call(a.api_url, uri, ".json", a.access_token, HTTP_POST, params, result)
}
func (a *APIClient) Auth(uri string, params map[string]interface{}, result interface{}) error {
return a.call(a.auth_url, uri, "", a.access_token, HTTP_POST, params, result)
}
func (a *APIClient) UPLOAD(uri string, params map[string]interface{}, result interface{}) error {
return a.call(a.api_url, uri, ".json", a.access_token, HTTP_UPLOAD, params, result)
}
type APIError struct {
When time.Time
ErrorCode int64
Message string
}
func (err *APIError) Error() string {
if err == nil {
return "Error with unknown reason"
}
return fmt.Sprintf("APIError When: %v ErrorMessage: %v ErrorCode: %v", err.When, err.Message, err.ErrorCode)
}