9
9
"net/http"
10
10
"net/url"
11
11
"os"
12
+ "path/filepath"
12
13
"strings"
13
14
14
15
"github.com/eat-pray-ai/yutu/pkg/utils"
@@ -30,9 +31,9 @@ const (
30
31
refreshTokenFailed = "failed to refresh token, please re-authenticate in cli"
31
32
parseSecretFailed = "failed to parse client secret"
32
33
33
- cacheTokenFile = "youtube.token.json "
34
- credentialFile = "client_secret.json "
35
- manualInputHint = `
34
+ browserOpenedHint = "Your browser has been opened to an authorization URL. yutu will resume once authorization has been provided. \n %s \n "
35
+ openBrowserHint = "It seems that your browser is not open. Go to the following link in your browser: \n %s \n "
36
+ manualInputHint = `
36
37
After completing the authorization flow, enter the authorization code on command line.
37
38
38
39
If you end up in an error page after completing the authorization flow,
@@ -69,26 +70,26 @@ func (s *svc) refreshClient() (client *http.Client) {
69
70
err := json .Unmarshal ([]byte (s .CacheToken ), authedToken )
70
71
if err != nil {
71
72
client , authedToken = s .newClient (config )
72
- if s .Cacheable {
73
- s .saveToken (cacheTokenFile , authedToken )
73
+ if s .tokenFile != "" {
74
+ s .saveToken (authedToken )
74
75
}
75
76
return client
76
77
}
77
78
78
79
if ! authedToken .Valid () {
79
80
tokenSource := config .TokenSource (s .ctx , authedToken )
80
81
authedToken , err = tokenSource .Token ()
81
- if err != nil && s .Cacheable {
82
+ if err != nil && s .tokenFile != "" {
82
83
client , authedToken = s .newClient (config )
83
- s .saveToken (cacheTokenFile , authedToken )
84
+ s .saveToken (authedToken )
84
85
return client
85
86
} else if err != nil {
86
87
slog .Error (refreshTokenFailed , "error" , err )
87
88
os .Exit (1 )
88
89
}
89
90
90
- if authedToken != nil && s .Cacheable {
91
- s .saveToken (cacheTokenFile , authedToken )
91
+ if authedToken != nil && s .tokenFile != "" {
92
+ s .saveToken (authedToken )
92
93
}
93
94
return config .Client (s .ctx , authedToken )
94
95
}
@@ -163,10 +164,7 @@ func (s *svc) startWebServer(redirectURL string) chan string {
163
164
}
164
165
165
166
func (s * svc ) getCodeFromPrompt (authURL string ) (code string ) {
166
- fmt .Printf (
167
- "It seems that your browser is not open. Go to the following " +
168
- "link in your browser:\n %s\n " , authURL ,
169
- )
167
+ fmt .Printf (openBrowserHint , authURL )
170
168
fmt .Print (manualInputHint )
171
169
_ , err := fmt .Scan (& code )
172
170
if err != nil {
@@ -187,19 +185,15 @@ func (s *svc) getTokenFromWeb(
187
185
188
186
var code string
189
187
if err := utils .OpenURL (authURL ); err == nil {
190
- fmt .Printf (
191
- "Your browser has been opened to an authorization URL. This " +
192
- "program will resume once authorization has been provided.\n %s\n " ,
193
- authURL ,
194
- )
188
+ fmt .Printf (browserOpenedHint , authURL )
195
189
code = <- codeCh
196
190
}
197
191
198
192
if code == "" {
199
193
code = s .getCodeFromPrompt (authURL )
200
194
}
201
195
202
- fmt . Printf ("Authorization code generated: %s \n " , code )
196
+ slog . Debug ("Authorization code generated" , "code " , code )
203
197
token , err := config .Exchange (context .TODO (), code )
204
198
if err != nil {
205
199
slog .Error (exchangeFailed , "error" , err )
@@ -209,10 +203,16 @@ func (s *svc) getTokenFromWeb(
209
203
return token
210
204
}
211
205
212
- func (s * svc ) saveToken (file string , token * oauth2.Token ) {
213
- f , err := os .OpenFile (file , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0600 )
206
+ func (s * svc ) saveToken (token * oauth2.Token ) {
207
+ dir := filepath .Dir (s .tokenFile )
208
+ if err := os .MkdirAll (dir , 0755 ); err != nil {
209
+ slog .Error (cacheTokenFailed , "dir" , dir , "error" , err )
210
+ os .Exit (1 )
211
+ }
212
+
213
+ f , err := os .OpenFile (s .tokenFile , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0600 )
214
214
if err != nil {
215
- slog .Error (cacheTokenFailed , "file" , file , "error" , err )
215
+ slog .Error (cacheTokenFailed , "file" , s . tokenFile , "error" , err )
216
216
os .Exit (1 )
217
217
}
218
218
@@ -224,4 +224,5 @@ func (s *svc) saveToken(file string, token *oauth2.Token) {
224
224
slog .Error (cacheTokenFailed , "error" , err )
225
225
os .Exit (1 )
226
226
}
227
+ slog .Info ("Token cached to file" , "file" , s .tokenFile )
227
228
}
0 commit comments