11package dev .lavalink .youtube .http ;
22
3- import com .grack .nanojson .JsonObject ;
4- import com .grack .nanojson .JsonParser ;
5- import com .grack .nanojson .JsonParserException ;
63import com .grack .nanojson .JsonWriter ;
74import com .sedmelluq .discord .lavaplayer .tools .DataFormatTools ;
85import com .sedmelluq .discord .lavaplayer .tools .ExceptionTools ;
6+ import com .sedmelluq .discord .lavaplayer .tools .JsonBrowser ;
97import com .sedmelluq .discord .lavaplayer .tools .io .HttpClientTools ;
108import com .sedmelluq .discord .lavaplayer .tools .io .HttpInterface ;
119import com .sedmelluq .discord .lavaplayer .tools .io .HttpInterfaceManager ;
12- import dev .lavalink .youtube .clients .skeleton .Client ;
1310import org .apache .http .client .methods .CloseableHttpResponse ;
1411import org .apache .http .client .methods .HttpPost ;
1512import org .apache .http .client .methods .HttpUriRequest ;
@@ -93,14 +90,14 @@ public boolean isOauthFetchContext(HttpClientContext context) {
9390 * be used instead.
9491 */
9592 private void initializeAccessToken () {
96- JsonObject response = fetchDeviceCode ();
93+ JsonBrowser response = fetchDeviceCode ();
9794
98- log .debug ("fetch device code response: {}" , JsonWriter . string ( response ));
95+ log .debug ("fetch device code response: {}" , response . format ( ));
9996
100- String verificationUrl = response .getString ("verification_url" );
101- String userCode = response .getString ("user_code" );
102- String deviceCode = response .getString ("device_code" );
103- long interval = response .getLong ("interval" ) * 1000 ;
97+ String verificationUrl = response .get ("verification_url" ). text ( );
98+ String userCode = response .get ("user_code" ). text ( );
99+ String deviceCode = response .get ("device_code" ). text ( );
100+ long interval = response .get ("interval" ). asLong ( 0 ) * 1000 ;
104101
105102 log .info ("==================================================" );
106103 log .info ("!!! DO NOT AUTHORISE WITH YOUR MAIN ACCOUNT, USE A BURNER !!!" );
@@ -118,7 +115,7 @@ private void initializeAccessToken() {
118115 * The returned payload will contain a user code and a device code, as well as a recommended poll interval,
119116 * which must be used to complete the flow.
120117 */
121- public JsonObject fetchDeviceCode () {
118+ public JsonBrowser fetchDeviceCode () {
122119 // @formatter:off
123120 String requestJson = JsonWriter .string ()
124121 .object ()
@@ -137,8 +134,8 @@ public JsonObject fetchDeviceCode() {
137134 try (HttpInterface httpInterface = getHttpInterface ();
138135 CloseableHttpResponse response = httpInterface .execute (request )) {
139136 HttpClientTools .assertSuccessWithContent (response , "device code fetch" );
140- return JsonParser . object (). from (response .getEntity ().getContent ());
141- } catch (IOException | JsonParserException e ) {
137+ return JsonBrowser . parse (response .getEntity ().getContent ());
138+ } catch (IOException e ) {
142139 throw ExceptionTools .toRuntimeException (e );
143140 }
144141 }
@@ -148,13 +145,13 @@ public JsonObject fetchDeviceCode() {
148145 * if the OAuth flow for the given device code has not yet been completed, or the device code is invalid.
149146 * @param deviceCode The device code obtained from {@link #fetchDeviceCode()}
150147 */
151- public JsonObject fetchRefreshToken (String deviceCode ) throws IOException {
148+ public JsonBrowser fetchRefreshToken (String deviceCode ) throws IOException {
152149 try (HttpInterface httpInterface = getHttpInterface ()) {
153150 return fetchRefreshToken (httpInterface , deviceCode );
154151 }
155152 }
156153
157- private JsonObject fetchRefreshToken (HttpInterface httpInterface , String deviceCode ) throws IOException {
154+ private JsonBrowser fetchRefreshToken (HttpInterface httpInterface , String deviceCode ) throws IOException {
158155 // @formatter:off
159156 String requestJson = JsonWriter .string ()
160157 .object ()
@@ -172,10 +169,10 @@ private JsonObject fetchRefreshToken(HttpInterface httpInterface, String deviceC
172169
173170 try (CloseableHttpResponse response = httpInterface .execute (request )) {
174171 HttpClientTools .assertSuccessWithContent (response , "oauth2 token fetch" );
175- JsonObject parsed = JsonParser . object (). from (response .getEntity ().getContent ());
176- log .debug ("oauth2 token fetch response: {}" , JsonWriter . string ( parsed ));
172+ JsonBrowser parsed = JsonBrowser . parse (response .getEntity ().getContent ());
173+ log .debug ("oauth2 token fetch response: {}" , parsed . format ( ));
177174 return parsed ;
178- } catch (IOException | JsonParserException e ) {
175+ } catch (IOException e ) {
179176 throw ExceptionTools .toRuntimeException (e );
180177 }
181178 }
@@ -184,10 +181,10 @@ private void pollForToken(String deviceCode, long interval) {
184181 try (HttpInterface httpInterface = getHttpInterface ()) {
185182 while (true ) {
186183 try {
187- JsonObject response = fetchRefreshToken (httpInterface , deviceCode );
184+ JsonBrowser response = fetchRefreshToken (httpInterface , deviceCode );
188185
189- if (response .has ("error" ) && ! response .isNull ("error" )) {
190- String error = response .getString ("error" );
186+ if (! response .get ("error" ).isNull ()) {
187+ String error = response .get ("error" ). text ( );
191188
192189 switch (error ) {
193190 case "authorization_pending" :
@@ -249,14 +246,10 @@ public void refreshAccessToken(boolean force) {
249246 return ;
250247 }
251248
252- try {
253- JsonObject json = createNewAccessToken (refreshToken );
254- updateTokens (json );
255- log .info ("YouTube access token refreshed successfully" );
256- log .debug ("YouTube access token is {} and refresh token is {}. Access token expires in {} seconds." , accessToken , refreshToken , json .getLong ("expires_in" ));
257- } catch (Exception e ) {
258- throw e ;
259- }
249+ JsonBrowser json = createNewAccessToken (refreshToken );
250+ updateTokens (json );
251+ log .info ("YouTube access token refreshed successfully" );
252+ log .debug ("YouTube access token is {} and refresh token is {}. Access token expires in {} seconds." , accessToken , refreshToken , json .get ("expires_in" ).asLong (300 ));
260253 }
261254 }
262255
@@ -267,7 +260,7 @@ public void refreshAccessToken(boolean force) {
267260 * @param refreshToken The refresh token to be included in the request.
268261 * @return The JSON response as a JsonObject.
269262 */
270- public JsonObject createNewAccessToken (String refreshToken ) {
263+ public JsonBrowser createNewAccessToken (String refreshToken ) {
271264 // @formatter:off
272265 String requestJson = JsonWriter .string ()
273266 .object ()
@@ -286,23 +279,25 @@ public JsonObject createNewAccessToken(String refreshToken) {
286279 try (HttpInterface httpInterface = getHttpInterface ();
287280 CloseableHttpResponse response = httpInterface .execute (request )) {
288281 HttpClientTools .assertSuccessWithContent (response , "oauth2 token fetch" );
289- JsonObject parsed = JsonParser . object (). from (response .getEntity ().getContent ());
282+ JsonBrowser parsed = JsonBrowser . parse (response .getEntity ().getContent ());
290283
291- if (parsed .has ("error" ) && ! parsed .isNull ("error" )) {
292- throw new RuntimeException ("Refreshing access token returned error " + parsed .getString ("error" ));
284+ if (! parsed .get ("error" ).isNull ()) {
285+ throw new RuntimeException ("Refreshing access token returned error " + parsed .get ("error" ). text ( ));
293286 }
294287
295288 return parsed ;
296- } catch (IOException | JsonParserException e ) {
289+ } catch (IOException e ) {
297290 throw ExceptionTools .toRuntimeException (e );
298291 }
299292 }
300293
301- private void updateTokens (JsonObject json ) {
302- long tokenLifespan = json .getLong ("expires_in" );
303- tokenType = json .getString ("token_type" );
304- accessToken = json .getString ("access_token" );
305- refreshToken = json .getString ("refresh_token" , refreshToken );
294+ private void updateTokens (JsonBrowser json ) {
295+ JsonBrowser newRefreshToken = json .get ("refresh_token" );
296+
297+ long tokenLifespan = json .get ("expires_in" ).asLong (300 );
298+ tokenType = json .get ("token_type" ).text ();
299+ accessToken = json .get ("access_token" ).text ();
300+ refreshToken = newRefreshToken .isNull () ? refreshToken : newRefreshToken .text ();
306301 tokenExpires = System .currentTimeMillis () + (tokenLifespan * 1000 ) - 60000 ;
307302
308303 log .debug ("OAuth access token is {} and refresh token is {}. Access token expires in {} seconds." , accessToken , refreshToken , tokenLifespan );
0 commit comments