authorization flow work
This commit is contained in:
@@ -57,14 +57,38 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// If Twitter is configured but has no access token, run auth before the TUI.
|
||||
// If Twitter is configured but has no access token, try a silent refresh
|
||||
// first (if a refresh token is stored), then fall back to full re-auth.
|
||||
if cfg.Credentials.Twitter.ClientID != "" && !cfg.HasTwitterPost() {
|
||||
fmt.Println("No Twitter access token found. Starting authorization...")
|
||||
runAuth(cfgPath)
|
||||
cfg, err = config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error reloading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
tw := cfg.Credentials.Twitter
|
||||
if tw.OAuth2RefreshToken != "" {
|
||||
result, err := auth.RefreshTwitterToken(context.Background(), tw.ClientID, tw.ClientSecret, tw.OAuth2RefreshToken)
|
||||
if err == nil {
|
||||
if saveErr := config.UpdateOAuth2Tokens(cfgPath, result.AccessToken, result.RefreshToken); saveErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not save refreshed token: %v\n", saveErr)
|
||||
}
|
||||
cfg, err = config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error reloading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Token refresh failed (%v), re-authorizing...\n", err)
|
||||
runAuth(cfgPath)
|
||||
cfg, err = config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error reloading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Println("No Twitter access token found. Starting authorization...")
|
||||
runAuth(cfgPath)
|
||||
cfg, err = config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error reloading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,7 +235,7 @@ func runAuth(cfgPath string) {
|
||||
fmt.Printf("Authorized as @%s\n", username)
|
||||
}
|
||||
|
||||
if err := config.UpdateOAuth2Token(cfgPath, result.AccessToken); err != nil {
|
||||
if err := config.UpdateOAuth2Tokens(cfgPath, result.AccessToken, result.RefreshToken); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not save token automatically: %v\n", err)
|
||||
fmt.Printf("\nAdd this to %s manually:\n oauth2_access_token: \"%s\"\n", cfgPath, result.AccessToken)
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user