diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-09 23:03:23 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-09 23:03:23 +0300 |
| commit | a69e38e109f44b85ac068acabe639f48939f1525 (patch) | |
| tree | 3b02ef29a43bf1aabbeecd7f316fa56235d85b7d /internal | |
| parent | 6622cc6402df56dd416e3ca3894d5b208fa26077 (diff) | |
can re-create token when unauthorized
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 31 | ||||
| -rw-r--r-- | internal/platforms/linkedin/oauth2/oauth2.go | 3 |
2 files changed, 24 insertions, 10 deletions
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 5b07077..ad70af0 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -4,8 +4,10 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" + "log" "net/http" "codeberg.org/snonux/gos/internal/config" @@ -13,23 +15,33 @@ import ( "codeberg.org/snonux/gos/internal/platforms/linkedin/oauth2" ) +var errUnauthorized = errors.New("unauthorized access, refresh or create token?") + // TODO: Also implemebt a Text Platform output, which then laster can be // processed by Gemtexter as a page func Post(ctx context.Context, args config.Args, ent entry.Entry) error { - content, err := ent.Content() - if err != nil { - return nil + err := post(ctx, args, ent) + if errors.Is(err, errUnauthorized) { + log.Println(err, "=> trying to refresh LinkedIn access token") + args.Secrets.LinkedInAccessToken = "" // Reset the token + return post(ctx, args, ent) } + return err +} +func post(ctx context.Context, args config.Args, ent entry.Entry) error { personID, accessToken, err := oauth2.LinkedInCreds(args) if err != err { return err } - - return post(personID, accessToken, content) + content, err := ent.Content() + if err != nil { + return nil + } + return callLinkedInAPI(personID, accessToken, content) } -func post(personID, accessToken, message string) error { +func callLinkedInAPI(personID, accessToken, message string) error { const url = "https://api.linkedin.com/v2/posts" post := map[string]interface{}{ @@ -68,7 +80,10 @@ func post(personID, accessToken, message string) error { if resp.StatusCode != http.StatusCreated { body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("Failed to post to LinkedIn. Status: %s\n%s\n\n", resp.Status, body) + err = fmt.Errorf("failed to post to LinkedIn. Status: %s\n%s\n", resp.Status, body) + if resp.StatusCode == http.StatusUnauthorized { + err = errors.Join(err, errUnauthorized) + } } - return nil + return err } diff --git a/internal/platforms/linkedin/oauth2/oauth2.go b/internal/platforms/linkedin/oauth2/oauth2.go index bbc60fb..21caf92 100644 --- a/internal/platforms/linkedin/oauth2/oauth2.go +++ b/internal/platforms/linkedin/oauth2/oauth2.go @@ -93,7 +93,6 @@ func oauthCallbackHandler(w http.ResponseWriter, r *http.Request) { func LinkedInCreds(args config.Args) (string, string, error) { secrets := args.Secrets if secrets.LinkedInAccessToken != "" && secrets.LinkedInPersonID != "" { - // TODO: Check, whether the access token is still valid. If not, get a new one. return secrets.LinkedInPersonID, secrets.LinkedInAccessToken, nil } @@ -104,7 +103,7 @@ func LinkedInCreds(args config.Args) (string, string, error) { Scopes: []string{"openid", "profile", "w_member_social"}, Endpoint: linkedin.Endpoint, } - errCh = make(chan error, 10) + errCh = make(chan error) http.HandleFunc("/", oauthIndexHandler) http.HandleFunc("/callback", oauthCallbackHandler) |
