]> Vexing Labs - dead-tooter.git/commitdiff
wip, moved errors out of pkg code, still not autorizing accounts.
authorAdam Shamblin <adam@vexingworkshop.com>
Mon, 12 Dec 2022 06:21:08 +0000 (23:21 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Mon, 12 Dec 2022 06:21:08 +0000 (23:21 -0700)
main.go
pkg/mastodon/account.go
pkg/mastodon/application.go

diff --git a/main.go b/main.go
index 8c18a42439382a3c7d122738fe3637a894b2b0de..ce3a4862fb8cf0b08015b3f400062c062c2babec 100644 (file)
--- a/main.go
+++ b/main.go
@@ -11,6 +11,22 @@ func main() {
                panic(err.Error())
        }
 
-       token := app.Login()
-       app.VerifyCredentials(token)
+       token, err := app.Login()
+       if err != nil {
+               panic(err.Error())
+       }
+
+       err = app.VerifyCredentials(token)
+       if err != nil {
+               panic(err.Error())
+       }
+
+       var account mastodon.Account
+       code := account.Authorize(app)
+       token, err = account.RequestToken(app, code)
+       if err != nil {
+               panic(err.Error())
+       }
+
+       account.VerifyCredentials(token)
 }
index a9240cdaf715ebe50949552d5c295c4d5edd2ede..96da9d9d133b8e9d9a67492d504b750ddf127578 100644 (file)
@@ -2,6 +2,7 @@ package mastodon
 
 import (
        "encoding/json"
+       "errors"
        "fmt"
        "io"
        "net/http"
@@ -10,6 +11,48 @@ import (
 )
 
 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) {
@@ -31,12 +74,15 @@ 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)
@@ -52,18 +98,60 @@ func (a *Account) RequestToken(app Application, code string) (token Token) {
 
        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
index 1b4fe5d7a7756134da1eb436c4964088ed65434f..a57c587239e183fb716331035b20c1e068e6b104 100644 (file)
@@ -2,7 +2,7 @@ package mastodon
 
 import (
        "encoding/json"
-       "fmt"
+       "errors"
        "io"
        "net/http"
        "net/url"
@@ -67,27 +67,39 @@ func (a *Application) Load(name string) error {
 
 // 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
@@ -95,34 +107,40 @@ func (a *Application) Login() (token Token) {
 
 // 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
 }