43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package androidpublisher
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/google"
|
|
apv3 "google.golang.org/api/androidpublisher/v3"
|
|
"google.golang.org/api/option"
|
|
)
|
|
|
|
// NewService 创建androidpublisher service
|
|
func NewService(ctx context.Context, jsonKey []byte, proxyUrl string) (*apv3.Service, error) {
|
|
c := &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
|
|
if proxyUrl != "" {
|
|
c.Transport = &http.Transport{
|
|
Proxy: func(request *http.Request) (*url.URL, error) {
|
|
return url.Parse(proxyUrl)
|
|
},
|
|
}
|
|
}
|
|
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, c)
|
|
conf, err := google.JWTConfigFromJSON(jsonKey, apv3.AndroidpublisherScope)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("google.JWTConfigFromJSON: %w", err)
|
|
}
|
|
val := conf.Client(ctx).Transport.(*oauth2.Transport)
|
|
_, err = val.Source.Token()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("val.Source.Token: %w", err)
|
|
}
|
|
service, err := apv3.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
|
|
return service, err
|
|
}
|