@@ -13,13 +13,23 @@ use std::{
13
13
} ;
14
14
use url:: Url ;
15
15
16
+ fn get_code ( redirect_url : & str ) -> AuthorizationCode {
17
+ let url = Url :: parse ( redirect_url) . unwrap ( ) ;
18
+ let code = url
19
+ . query_pairs ( )
20
+ . find ( |( key, _) | key == "code" )
21
+ . map ( |( _, code) | AuthorizationCode :: new ( code. into_owned ( ) ) )
22
+ . unwrap ( ) ;
23
+ code
24
+ }
25
+
16
26
fn get_authcode_stdin ( ) -> AuthorizationCode {
17
- println ! ( "Provide code " ) ;
27
+ println ! ( "Provide redirect URL " ) ;
18
28
let mut buffer = String :: new ( ) ;
19
29
let stdin = io:: stdin ( ) ; // We get `Stdin` here.
20
30
stdin. read_line ( & mut buffer) . unwrap ( ) ;
21
31
22
- AuthorizationCode :: new ( buffer. trim ( ) . into ( ) )
32
+ get_code ( buffer. trim ( ) )
23
33
}
24
34
25
35
fn get_authcode_listener ( socket_address : SocketAddr ) -> AuthorizationCode {
@@ -39,13 +49,7 @@ fn get_authcode_listener(socket_address: SocketAddr) -> AuthorizationCode {
39
49
reader. read_line ( & mut request_line) . unwrap ( ) ;
40
50
41
51
let redirect_url = request_line. split_whitespace ( ) . nth ( 1 ) . unwrap ( ) ;
42
- let url = Url :: parse ( & ( "http://localhost" . to_string ( ) + redirect_url) ) . unwrap ( ) ;
43
-
44
- let code = url
45
- . query_pairs ( )
46
- . find ( |( key, _) | key == "code" )
47
- . map ( |( _, code) | AuthorizationCode :: new ( code. into_owned ( ) ) )
48
- . unwrap ( ) ;
52
+ let code = get_code ( & ( "http://localhost" . to_string ( ) + redirect_url) ) ;
49
53
50
54
let message = "Go back to your terminal :)" ;
51
55
let response = format ! (
@@ -60,6 +64,7 @@ fn get_authcode_listener(socket_address: SocketAddr) -> AuthorizationCode {
60
64
61
65
// TODO: Return a Result?
62
66
// TODO: Pass in redirect_address instead since the redirect host depends on client ID?
67
+ // TODO: Pass in scopes.
63
68
pub fn get_access_token ( client_id : & str , redirect_port : u16 ) -> String {
64
69
// Must use host 127.0.0.1 with Spotify Desktop client ID.
65
70
let redirect_address = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , redirect_port) ;
0 commit comments