@@ -27,11 +27,11 @@ public LiveTokenManager(String cache, HttpClient httpClient) {
2727 public boolean verifyTokens () {
2828 LiveTokenCache tokenCache = getCache ();
2929
30- if (tokenCache .obtainedOn () == 0L ) {
30+ if (tokenCache .obtainedOn == 0L ) {
3131 return false ;
3232 }
3333
34- long expiry = tokenCache .obtainedOn () + (tokenCache .token () .expires_in () * 1000L );
34+ long expiry = tokenCache .obtainedOn + (tokenCache .token .expires_in * 1000L );
3535 boolean valid = (expiry - System .currentTimeMillis ()) > 1000 ;
3636
3737 if (!valid ) {
@@ -58,16 +58,16 @@ private void refreshToken() throws Exception {
5858 .POST (HttpRequest .BodyPublishers .ofString ("scope=" + Constants .SCOPE + "&client_id=" + Constants .AUTH_TITLE + "&grant_type=refresh_token&refresh_token=" + refreshToken ))
5959 .build ();
6060
61- LiveTokenResponse tokenResponse = Constants .GSON . fromJson (httpClient .send (tokenRequest , HttpResponse .BodyHandlers .ofString ()).body (), LiveTokenResponse .class );
61+ LiveTokenResponse tokenResponse = Constants .OBJECT_MAPPER . readValue (httpClient .send (tokenRequest , HttpResponse .BodyHandlers .ofString ()).body (), LiveTokenResponse .class );
6262 updateCache (tokenResponse );
6363 }
6464
6565 public String getAccessToken () {
66- return getCache ().token () .access_token () ;
66+ return getCache ().token .access_token ;
6767 }
6868
6969 private String getRefreshToken () {
70- return getCache ().token () .refresh_token () ;
70+ return getCache ().token .refresh_token ;
7171 }
7272
7373 public Future <String > authDeviceCode () {
@@ -80,30 +80,32 @@ public Future<String> authDeviceCode() {
8080 .POST (HttpRequest .BodyPublishers .ofString ("scope=" + Constants .SCOPE + "&client_id=" + Constants .AUTH_TITLE + "&response_type=device_code" ))
8181 .build ();
8282
83- LiveDeviceCodeResponse codeResponse = Constants . GSON . fromJson ( httpClient .send (codeRequest , HttpResponse .BodyHandlers .ofString ()). body (), LiveDeviceCodeResponse . class );
83+ HttpResponse < String > response = httpClient .send (codeRequest , HttpResponse .BodyHandlers .ofString ());
8484
85- long expireTime = System . currentTimeMillis () + ( codeResponse . expires_in () * 1000L );
85+ LiveDeviceCodeResponse codeResponse = Constants . OBJECT_MAPPER . readValue ( response . body (), LiveDeviceCodeResponse . class );
8686
87- System .out .println ("To sign in, use a web browser to open the page " + codeResponse .verification_uri () + " and enter the code " + codeResponse .user_code () + " to authenticate." );
87+ long expireTime = System .currentTimeMillis () + (codeResponse .expires_in * 1000L );
88+
89+ System .out .println ("To sign in, use a web browser to open the page " + codeResponse .verification_uri + " and enter the code " + codeResponse .user_code + " to authenticate." );
8890
8991 while (System .currentTimeMillis () < expireTime ) {
90- Thread .sleep (codeResponse .interval () * 1000L );
92+ Thread .sleep (codeResponse .interval * 1000L );
9193
9294 HttpRequest tokenRequest = HttpRequest .newBuilder ()
9395 .uri (Constants .LIVE_TOKEN_REQUEST )
9496 .header ("Content-Type" , "application/x-www-form-urlencoded" )
95- .POST (HttpRequest .BodyPublishers .ofString ("device_code=" + codeResponse .device_code () + "&client_id=" + Constants .AUTH_TITLE + "&grant_type=urn:ietf:params:oauth:grant-type:device_code" ))
97+ .POST (HttpRequest .BodyPublishers .ofString ("device_code=" + codeResponse .device_code + "&client_id=" + Constants .AUTH_TITLE + "&grant_type=urn:ietf:params:oauth:grant-type:device_code" ))
9698 .build ();
9799
98- LiveTokenResponse tokenResponse = Constants .GSON . fromJson (httpClient .send (tokenRequest , HttpResponse .BodyHandlers .ofString ()).body (), LiveTokenResponse .class );
100+ LiveTokenResponse tokenResponse = Constants .OBJECT_MAPPER . readValue (httpClient .send (tokenRequest , HttpResponse .BodyHandlers .ofString ()).body (), LiveTokenResponse .class );
99101
100- if (tokenResponse .error () != null && !tokenResponse .error () .isEmpty ()) {
101- if (!tokenResponse .error () .equals ("authorization_pending" )) {
102+ if (tokenResponse .error != null && !tokenResponse .error .isEmpty ()) {
103+ if (!tokenResponse .error .equals ("authorization_pending" )) {
102104 // TODO handle error
103105 }
104106 } else {
105107 updateCache (tokenResponse );
106- completableFuture .complete (tokenResponse .access_token () );
108+ completableFuture .complete (tokenResponse .access_token );
107109 break ;
108110 }
109111 }
@@ -116,15 +118,15 @@ public Future<String> authDeviceCode() {
116118
117119 private LiveTokenCache getCache () {
118120 try {
119- return Constants .GSON . fromJson (Files .readString (cache ), LiveTokenCache .class );
121+ return Constants .OBJECT_MAPPER . readValue (Files .readString (cache ), LiveTokenCache .class );
120122 } catch (IOException e ) {
121- return new LiveTokenCache (0 , new LiveTokenResponse ( null , null , 0 , null , null , null , null ) );
123+ return new LiveTokenCache ();
122124 }
123125 }
124126
125127 private void updateCache (LiveTokenResponse tokenResponse ) {
126128 try (FileWriter writer = new FileWriter (cache .toString (), StandardCharsets .UTF_8 )) {
127- Constants .GSON . toJson ( new LiveTokenCache (System .currentTimeMillis (), tokenResponse ), writer );
129+ Constants .OBJECT_MAPPER . writeValue ( writer , new LiveTokenCache (System .currentTimeMillis (), tokenResponse ));
128130 } catch (IOException e ) {
129131 e .printStackTrace ();
130132 // TODO Handle properly
0 commit comments