import (
"encoding/json"
+ "errors"
"fmt"
"io"
"net/http"
)
type Account struct {
+ ID string `json:"id"`
+ UserName string `json:"username"`
+ Acct string `json:"acct"`
+ DisplayName string `json:"display_name"`
+ Locked bool `json:"locked"`
+ Bot bool `json:"bot"`
+ CreatedAt string `json:"created_at"`
+ Note string `json:"note"`
+ URL string `json:"url"`
+ Avatar string `json:"avatar"`
+ AvatarStatic string `json:"avatar_static"`
+ Header string `json:"header"`
+ HeaderStatic string `json:"header_static"`
+ FollowersCount int `json:"followers_count"`
+ FollowingCount int `json:"following_count"`
+ StatusesCount int `json:"statuses_count"`
+ LastStatusAt string `json:"last_status_at"`
+ Source Source `json:"source"`
+ Emojis []Emoji `json:"emojis"`
+ Fields []Field `json:"fields"`
+}
+
+type Source struct {
+ Privacy string `json:"privacy"`
+ Sensitive bool `json:"sensitive"`
+ Language string `json:"language"`
+ Note string `json:"note"`
+ Fields []Field `json:"fields"`
+ FollowRequestsCount int `json:"follow_requests_count"`
+}
+
+type Field struct {
+ Name string `json:"name"`
+ Value string `json:"value"`
+ VerifiedAt string `json:"verified_at"`
+}
+
+type Emoji struct {
+ ShortCode string `json:"shortcode"`
+ Url string `json:"url"`
+ StaticUrl string `json:"static_url"`
+ VisibleInPicker bool `json:"visible_in_picker"`
}
func (a *Account) Authorize(app Application) (code string) {
}
fmt.Print("Enter returned code: ")
- fmt.Scanln(&code)
+ _, err = fmt.Scanln(&code)
+ if err != nil {
+ panic(err.Error())
+ }
return
}
-func (a *Account) RequestToken(app Application, code string) (token Token) {
+func (a *Account) RequestToken(app Application, code string) (token Token, err error) {
v := url.Values{}
v.Set("client_id", app.ClientID)
v.Set("code", code)
resp, err := http.PostForm(u.String(), v)
if err != nil {
- panic(err.Error())
+ return
+ }
+ if resp.StatusCode != 200 {
+ err = errors.New(resp.Status)
+ return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
- panic(err.Error())
+ return
}
err = json.Unmarshal(body, &token)
if err != nil {
- panic(err.Error())
+ return
+ }
+
+ return
+}
+
+func (a *Account) VerifyCredentials(token Token) (err error) {
+ client := &http.Client{}
+
+ u := url.URL{
+ Host: "hackers.town",
+ Path: "api/v1/accounts/verify_credentials",
+ }
+ 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, a)
+ if err != nil {
+ return
}
return
import (
"encoding/json"
- "fmt"
+ "errors"
"io"
"net/http"
"net/url"
// Login authenticates the application to the Mastodon API, returning
// a bearer token to be used with future requests.
-func (a *Application) Login() (token Token) {
- resp, err := http.PostForm("https://hackers.town/oauth/token",
- url.Values{
- "client_id": {a.ClientID},
- "client_secret": {a.ClientSecret},
- "redirect_uri": {a.RedirectURI},
- "grant_type": {"client_credentials"},
- })
+func (a *Application) Login() (token Token, err error) {
+ v := url.Values{}
+ v.Set("client_id", a.ClientID)
+ v.Set("client_secret", a.ClientSecret)
+ v.Set("redirect_uri", a.RedirectURI)
+ v.Set("grant_type", "client_credentials")
+
+ u := url.URL{
+ Host: "hackers.town",
+ Path: "oauth/token",
+ RawQuery: v.Encode(),
+ }
+ u.Scheme = "https"
+
+ resp, err := http.PostForm(u.String(), v)
if err != nil {
- panic(err.Error())
+ return
+ }
+ if resp.StatusCode != 200 {
+ err = errors.New(resp.Status)
+ return
}
+
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
- panic(err.Error())
+ return
}
err = json.Unmarshal(body, &token)
if err != nil {
- panic(err.Error())
+ return
}
return
// VerifyCredentials accepts a Token object and validates the contained
// token against the Mastodon API.
-func (a *Application) VerifyCredentials(token Token) {
+func (a *Application) VerifyCredentials(token Token) (err error) {
client := &http.Client{}
- req, err := http.NewRequest("GET",
- "https://hackers.town/api/v1/apps/verify_credentials", nil)
+ u := url.URL{
+ Host: "hackers.town",
+ Path: "api/v1/apps/verify_credentials",
+ }
+ u.Scheme = "https"
+
+ req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
- panic(err.Error())
+ return
}
req.Header.Add("Authorization", "Bearer "+token.AccessToken)
resp, err := client.Do(req)
if err != nil {
- panic(err.Error())
+ return
+ }
+ if resp.StatusCode != 200 {
+ err = errors.New(resp.Status)
+ return
}
defer resp.Body.Close()
- fmt.Printf("%s", resp.Status)
-
body, err := io.ReadAll(resp.Body)
if err != nil {
- panic(err.Error())
+ return
}
- var dummy Application
- err = json.Unmarshal(body, &dummy)
+ err = json.Unmarshal(body, a)
if err != nil {
- panic(err.Error())
+ return
}
- fmt.Printf("%+v", dummy)
+ return
}