From da1a8236fb8e3acc583a8c2db130a7d31fe6de21 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Tue, 13 Dec 2022 17:19:27 -0700 Subject: [PATCH] Working call to get followers --- main.go | 7 +++++- pkg/mastodon/account.go | 50 ++++++++++++++++++++++++++++++++++--- pkg/mastodon/application.go | 5 ++-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 680f31a..d030fd8 100644 --- a/main.go +++ b/main.go @@ -35,5 +35,10 @@ func main() { panic(err.Error()) } - fmt.Printf("%+v\n", token) + followers, err := account.GetFollowers(token) + if err != nil { + panic(err.Error()) + } + + fmt.Printf("%+v\n", followers) } diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index b651b1b..34c5ffd 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -62,7 +62,7 @@ func (a *Account) Authorize(app Application) (code string) { v.Set("redirect_uri", RedirectUris) u := url.URL{ - Host: "hackers.town", + Host: mastohost, Path: "oauth/authorize", RawQuery: v.Encode(), } @@ -91,7 +91,7 @@ func (a *Account) RequestToken(app Application, code string) (token Token, err e v.Set("grant_type", "authorization_code") u := url.URL{ - Host: "hackers.town", + Host: mastohost, Path: "oauth/token", RawQuery: v.Encode(), } @@ -124,7 +124,7 @@ func (a *Account) VerifyCredentials(token Token) (err error) { client := &http.Client{} u := url.URL{ - Host: "hackers.town", + Host: mastohost, Path: "api/v1/accounts/verify_credentials", } u.Scheme = "https" @@ -157,3 +157,47 @@ func (a *Account) VerifyCredentials(token Token) (err error) { return } + +func (a *Account) GetFollowers(token Token) (followers []Account, err error) { + client := &http.Client{} + + id := url.PathEscape(a.ID) + path, err := url.JoinPath("api/v1/accounts", id, "followers") + if err != nil { + return + } + + u := url.URL{ + Host: mastohost, + Path: path, + } + u.Scheme = "https" + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return + } + req.Header.Add("Authorization", "Bearer "+token.AccessToken) + + resp, err := client.Do(req) + if err != nil { + return + } + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return + } + + err = json.Unmarshal(body, &followers) + if err != nil { + return + } + + return +} diff --git a/pkg/mastodon/application.go b/pkg/mastodon/application.go index a57c587..b425047 100644 --- a/pkg/mastodon/application.go +++ b/pkg/mastodon/application.go @@ -10,6 +10,7 @@ import ( ) const configdir = "foodir/" +const mastohost = "hackers.town" // Token struct contains the data returned by the Application login request. type Token struct { @@ -75,7 +76,7 @@ func (a *Application) Login() (token Token, err error) { v.Set("grant_type", "client_credentials") u := url.URL{ - Host: "hackers.town", + Host: mastohost, Path: "oauth/token", RawQuery: v.Encode(), } @@ -111,7 +112,7 @@ func (a *Application) VerifyCredentials(token Token) (err error) { client := &http.Client{} u := url.URL{ - Host: "hackers.town", + Host: mastohost, Path: "api/v1/apps/verify_credentials", } u.Scheme = "https" -- 2.39.5