mi-v1/cmd/mi/patreon.go

48 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-01-11 18:38:50 +00:00
package main
import (
"context"
2020-01-11 19:11:30 +00:00
"flag"
2020-01-11 18:38:50 +00:00
"time"
"golang.org/x/oauth2"
"gopkg.in/mxpv/patreon-go.v1"
)
2020-01-11 19:11:30 +00:00
var (
// Patreon
patreonClientID = flag.String("patreon-client-id", "", "Patreon client ID")
patreonClientSecret = flag.String("patreon-client-secret", "", "Patreon client secret")
patreonAccessToken = flag.String("patreon-access-token", "", "Patreon access token")
patreonRefreshToken = flag.String("patreon-refresh-token", "", "Patreon refresh token")
)
2020-01-11 18:38:50 +00:00
func makePatreon() (*patreon.Client, error) {
config := oauth2.Config{
ClientID: *patreonClientID,
ClientSecret: *patreonClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: patreon.AuthorizationURL,
TokenURL: patreon.AccessTokenURL,
},
Scopes: []string{"users", "pledges-to-me", "my-campaign"},
}
token := oauth2.Token{
AccessToken: *patreonAccessToken,
RefreshToken: *patreonRefreshToken,
// Must be non-nil, otherwise token will not be expired
Expiry: time.Now().Add(-24 * time.Hour),
}
tc := config.Client(context.Background(), &token)
client := patreon.NewClient(tc)
_, err := client.FetchUser()
if err != nil {
return nil, err
}
return client, nil
}