Skip to content

Commit

Permalink
feat: store the refresh token so the OAuth flow isn't required
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno authored Jun 19, 2024
1 parent 37f3911 commit 3a24eda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
24 changes: 17 additions & 7 deletions cobra/cmd/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,38 @@ type Blogger struct {
BlogUrl string
}

func (b Blogger) authorize(clientId string, clientSecret string, providedRefreshToken string) (string, error) {
// Return the access token, refresh token (if one was not provided), and an error (if one occurred).
// The access and refresh tokens are only returned if an error did not occur.
// In Google Cloud, create OAuth client credentials for a desktop app and enable the Blogger API.
func (b Blogger) authorize(clientId string, clientSecret string, providedRefreshToken string) (string, string, error) {
oauthConfig := oauth.Config{
ClientID: clientId,
ClientSecret: clientSecret,
Port: "8080",
}
var refreshToken string
var err error
if providedRefreshToken == "" {
if providedRefreshToken != "" {
log.Info("Using provided refresh token")
refreshToken = providedRefreshToken
} else {
log.Info("No refresh token provided, starting OAuth flow")
refreshToken, err = oauth.GetGoogleRefreshToken(oauthConfig)
if err != nil {
return "", err
return "", "", err
}
} else {
refreshToken = providedRefreshToken
}
accessToken, err := oauth.GetGoogleAccessToken(oauthConfig, refreshToken)
if err != nil {
return "", err
// Not returning the refresh token because it may have been invalid
return "", "", err
}
log.Info("", "access token", accessToken)
return accessToken, nil
if providedRefreshToken != "" {
return accessToken, providedRefreshToken, nil
}
return accessToken, refreshToken, nil

}
func (b Blogger) getBlogId(accessToken string) (string, error) {
client := resty.New()
Expand Down
22 changes: 19 additions & 3 deletions cobra/cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,25 @@ var publishCmd = &cobra.Command{
}
// If the refresh token exists in Viper, pass that to Blogger.Authorize. Otherwise, pass an empty string
refreshToken := viper.GetString("google-refresh-token")
var accessToken string
var err error
if refreshToken == "" {
log.Warn("No refresh token found in Viper")
accessToken, refreshToken, err = blogger.authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), "")
if err != nil {
log.Fatal(err)
}
// Write the refresh token to the config file
log.Info("Writing refresh token to Viper")
viper.Set("google-refresh-token", refreshToken)
err = viper.WriteConfig()
if err != nil {
log.Fatal(err)
}
} else {
log.Info("Found refresh token in Viper")
accessToken, _, err = blogger.authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), refreshToken)
}
accessToken, err := blogger.authorize(viper.GetString("google-client-id"), viper.GetString("google-client-secret"), refreshToken)
if err != nil {
log.Fatal(err)
}
Expand All @@ -125,12 +138,15 @@ func init() {
publishCmd.Flags().String("google-client-id", "", "Google OAuth client ID")
publishCmd.Flags().String("google-client-secret", "", "Google OAuth client secret")
publishCmd.Flags().String("google-refresh-token", "", "Google OAuth refresh token")
// Keep in mind that if the refresh token is not set in the config file, the program will request one
// It will then write the refresh token to the config file, along with any flags or env vars that have been set.
// You could always go back and remove those lines and continue using environment variables or flags as it won't write to the config file as long as the refresh token is set
// Allow the OAuth stuff to be set via viper
viper.BindPFlag("google-client-id", publishCmd.Flags().Lookup("google-client-id"))
viper.BindPFlag("google-client-secret", publishCmd.Flags().Lookup("google-client-secret"))
// viper.BindPFlag("google-refresh-token", publishCmd.Flags().Lookup("google-refresh-token"))
viper.BindPFlag("google-refresh-token", publishCmd.Flags().Lookup("google-refresh-token"))
// Keep in mind that these should be prefixed with CROSS_BLOGGER
viper.BindEnv("google-client-id", "CROSS_BLOGGER_GOOGLE_CLIENT_ID")
viper.BindEnv("google-client-secret", "CROSS_BLOGGER_GOOGLE_CLIENT_SECRET")
// viper.BindEnv("google-refresh-token", "GOOGLE_REFRESH_TOKEN")
viper.BindEnv("google-refresh-token", "GOOGLE_REFRESH_TOKEN")
}

0 comments on commit 3a24eda

Please sign in to comment.