-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
55 lines (50 loc) · 1.42 KB
/
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
// Package weibo 新浪微博 SDK
package weibo
import (
"net/http"
"net/http/cookiejar"
"time"
)
// RespError 微博接口的错误结果返回结构
type RespError struct {
Error string `json:"error"`
ErrorCode int `json:"error_code"`
Request string `json:"request"`
}
// HTTPTimeout 请求超时时间 默认 10 秒
var HTTPTimeout time.Duration = time.Second * 10
// Weibo 实例,在其上实现各类接口
type Weibo struct {
client *http.Client
appkey string
appsecret string
redirecturi string
username string
passwd string
userAgent string
crackPinFuncs []CrackPinFunc
token string
}
// New 创建Weibo实例
// appkey 微博开放平台应用的 appkey
// appsecret 微博开放平台应用的 appsecret
// username 需要发微博的微博登录账号,用于模拟登录直接获取授权码
// password 需要发微博的微博登录密码,用于模拟登录直接获取授权码
// redirecturi 微博开发平台应用的回调 URL
func New(appkey, appsecret, username, passwd, redirecturi string) *Weibo {
// 设置cookiejar后续请求会自动带cookie保持会话
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
Timeout: HTTPTimeout,
}
return &Weibo{
client: client,
appkey: appkey,
appsecret: appsecret,
redirecturi: redirecturi,
username: username,
passwd: passwd,
userAgent: RandUA(),
}
}