-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.api.go
136 lines (118 loc) · 3.45 KB
/
oauth.api.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
// Code generated by protoc-gen-go_api(github.com/dev-openapi/protoc-gen-go_api version=v1.0.5). DO NOT EDIT.
// source: bilibili-webapp/oauth.proto
package bilibili_webapp
import (
context "context"
fmt "fmt"
io "io"
json "encoding/json"
bytes "bytes"
http "net/http"
strings "strings"
url "net/url"
multipart "mime/multipart"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = context.Background
var _ = http.NewRequest
var _ = io.Copy
var _ = bytes.Compare
var _ = json.Marshal
var _ = strings.Compare
var _ = fmt.Errorf
var _ = url.Parse
var _ = multipart.ErrMessageTooLarge
// Client API for Oauth service
type OauthService interface {
// GetAccessToken 获取AccessToken https://openhome.bilibili.com/doc/4/eaf0e2b5-bde9-b9a0-9be1-019bb455701c
GetAccessToken(ctx context.Context, in *GetAccessTokenReq, opts ...Option) (*GetAccessTokenRes, error)
// RefreshToken 刷新token https://openhome.bilibili.com/doc/4/eaf0e2b5-bde9-b9a0-9be1-019bb455701c
RefreshToken(ctx context.Context, in *RefreshTokenReq, opts ...Option) (*RefreshTokenRes, error)
}
type oauthService struct {
// opts
opts *Options
}
func NewOauthService(opts ...Option) OauthService {
opt := newOptions(opts...)
if len(opt.addr) <= 0 {
opt.addr = "https://api.bilibili.com"
}
return &oauthService {
opts: opt,
}
}
func (c *oauthService) GetAccessToken(ctx context.Context, in *GetAccessTokenReq, opts ...Option) (*GetAccessTokenRes, error) {
var res GetAccessTokenRes
// options
opt := buildOptions(c.opts, opts...)
headers := make(map[string]string)
// route
rawURL := fmt.Sprintf("%s/x/account-oauth2/v1/token", opt.addr)
// body
req, err := http.NewRequest("POST", rawURL, nil)
if err != nil {
return nil, err
}
params := req.URL.Query()
if in.GetClientId() != "" {
params.Add("client_id", fmt.Sprintf("%v", in.GetClientId()))
}
if in.GetClientSecret() != "" {
params.Add("client_secret", fmt.Sprintf("%v", in.GetClientSecret()))
}
if in.GetCode() != "" {
params.Add("code", fmt.Sprintf("%v", in.GetCode()))
}
if in.GetGrantType() != "" {
params.Add("grant_type", fmt.Sprintf("%v", in.GetGrantType()))
}
req.URL.RawQuery = params.Encode()
// header
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := opt.DoRequest(ctx, opt.client, req)
if err != nil {
return nil, err
}
err = opt.DoResponse(ctx, resp, &res)
return &res, err
}
func (c *oauthService) RefreshToken(ctx context.Context, in *RefreshTokenReq, opts ...Option) (*RefreshTokenRes, error) {
var res RefreshTokenRes
// options
opt := buildOptions(c.opts, opts...)
headers := make(map[string]string)
// route
rawURL := fmt.Sprintf("%s/x/account-oauth2/v1/refresh_token", opt.addr)
// body
req, err := http.NewRequest("POST", rawURL, nil)
if err != nil {
return nil, err
}
params := req.URL.Query()
if in.GetClientId() != "" {
params.Add("client_id", fmt.Sprintf("%v", in.GetClientId()))
}
if in.GetClientSecret() != "" {
params.Add("client_secret", fmt.Sprintf("%v", in.GetClientSecret()))
}
if in.GetGrantType() != "" {
params.Add("grant_type", fmt.Sprintf("%v", in.GetGrantType()))
}
if in.GetRefreshToken() != "" {
params.Add("refresh_token", fmt.Sprintf("%v", in.GetRefreshToken()))
}
req.URL.RawQuery = params.Encode()
// header
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := opt.DoRequest(ctx, opt.client, req)
if err != nil {
return nil, err
}
err = opt.DoResponse(ctx, resp, &res)
return &res, err
}