mi-v1/cmd/mi/patreon.go

48 lines
1.1 KiB
Go

package main
import (
"context"
"flag"
"time"
"golang.org/x/oauth2"
"gopkg.in/mxpv/patreon-go.v1"
)
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")
)
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
}