panic(err.Error())
}
- fmt.Printf("%+v\n", token)
+ followers, err := account.GetFollowers(token)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ fmt.Printf("%+v\n", followers)
}
v.Set("redirect_uri", RedirectUris)
u := url.URL{
- Host: "hackers.town",
+ Host: mastohost,
Path: "oauth/authorize",
RawQuery: v.Encode(),
}
v.Set("grant_type", "authorization_code")
u := url.URL{
- Host: "hackers.town",
+ Host: mastohost,
Path: "oauth/token",
RawQuery: v.Encode(),
}
client := &http.Client{}
u := url.URL{
- Host: "hackers.town",
+ Host: mastohost,
Path: "api/v1/accounts/verify_credentials",
}
u.Scheme = "https"
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
+}
)
const configdir = "foodir/"
+const mastohost = "hackers.town"
// Token struct contains the data returned by the Application login request.
type Token struct {
v.Set("grant_type", "client_credentials")
u := url.URL{
- Host: "hackers.town",
+ Host: mastohost,
Path: "oauth/token",
RawQuery: v.Encode(),
}
client := &http.Client{}
u := url.URL{
- Host: "hackers.town",
+ Host: mastohost,
Path: "api/v1/apps/verify_credentials",
}
u.Scheme = "https"