/
youtube.go
executable file
·78 lines (71 loc) · 2.11 KB
/
youtube.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
package youtube
// youtube的auth方法
// https://godoc.org/google.golang.org/api/youtube/v3#hdr-Other_authentication_options
import (
"context"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/option"
htransport "google.golang.org/api/transport/http"
"google.golang.org/api/youtube/v3"
"net"
"net/http"
"net/url"
"time"
)
type (
Youtube struct {
service *youtube.Service
}
)
// https://github.com/googleapis/google-api-go-client/issues/434#issuecomment-563428458
func newServiceWithProxy(ctx context.Context, proxy string, opts ...option.ClientOption) (*youtube.Service, error) {
scopesOption := option.WithScopes(
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.force-ssl",
"https://www.googleapis.com/auth/youtube.readonly",
"https://www.googleapis.com/auth/youtube.upload",
"https://www.googleapis.com/auth/youtubepartner",
"https://www.googleapis.com/auth/youtubepartner-channel-audit",
)
// NOTE: prepend, so we don't override user-specified scopes.
opts = append([]option.ClientOption{scopesOption}, opts...)
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
}
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err != nil {
return nil, err
}
baseTransportWithProxy := &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client.Transport.(*transport.APIKey).Transport = baseTransportWithProxy
}
s, err := youtube.New(client)
if err != nil {
return nil, err
}
if endpoint != "" {
s.BasePath = endpoint
}
return s, nil
}
// NewYoutubeService create youtube client
func NewWithKey(key, proxy string) (*Youtube, error) {
service, err := newServiceWithProxy(context.Background(), proxy, option.WithAPIKey(key))
if err != nil {
return nil, err
}
return &Youtube{service: service}, nil
}