mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-02 22:32:19 +00:00
Handle registering the app
This commit is contained in:
parent
7e82fd4eb6
commit
a2761fac3e
2 changed files with 23 additions and 4 deletions
|
@ -17,7 +17,7 @@ var (
|
||||||
MASTODON_INSTANCE_URL string
|
MASTODON_INSTANCE_URL string
|
||||||
MASTODON_USERNAME string
|
MASTODON_USERNAME string
|
||||||
MASTODON_PASSWORD string
|
MASTODON_PASSWORD string
|
||||||
|
MASTODON_CLIENT_NAME = "go-mastodon-stats"
|
||||||
// UserCount metric name
|
// UserCount metric name
|
||||||
METRICNAME_USERCOUNT = "userCount"
|
METRICNAME_USERCOUNT = "userCount"
|
||||||
|
|
||||||
|
|
|
@ -8,18 +8,37 @@ import (
|
||||||
"github.com/mattn/go-mastodon"
|
"github.com/mattn/go-mastodon"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func registerApp() (string, string, error) {
|
||||||
|
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
|
||||||
|
Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL),
|
||||||
|
ClientName: MASTODON_CLIENT_NAME,
|
||||||
|
Scopes: "read write follow",
|
||||||
|
Website: "https://github.com/mattn/go-mastodon",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
return app.ClientID, app.ClientSecret, nil
|
||||||
|
}
|
||||||
|
|
||||||
func postToMastodon(metrics []metric) {
|
func postToMastodon(metrics []metric) {
|
||||||
if MASTODON_INSTANCE_URL == "" {
|
if MASTODON_INSTANCE_URL == "" {
|
||||||
log.Printf("Skipping posting to mastodon. Missing configuration")
|
log.Printf("Skipping posting to mastodon. Missing configuration")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientId, clientSecret, err := registerApp()
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c := mastodon.NewClient(&mastodon.Config{
|
c := mastodon.NewClient(&mastodon.Config{
|
||||||
Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL),
|
Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL),
|
||||||
ClientID: "go-mastodon-stats",
|
ClientID: clientId,
|
||||||
ClientSecret: "randomgeneratedstringthatimnotsureisneeded",
|
ClientSecret: clientSecret,
|
||||||
})
|
})
|
||||||
err := c.Authenticate(context.Background(), MASTODON_USERNAME, MASTODON_PASSWORD)
|
err = c.Authenticate(context.Background(), MASTODON_USERNAME, MASTODON_PASSWORD)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Invalid mastodon credentials?")
|
log.Println("Invalid mastodon credentials?")
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue