@@ -111,7 +111,7 @@ class GoogleSignIn {
111111 }
112112
113113 parsedOptions.optJSONArray(" scopes" )?.let {
114- for (i in 0 until it.length()) {
114+ for (i in 0 until it.length()) {
115115 builder.requestScopes(Scope (it.getString(i)))
116116 }
117117 }
@@ -267,6 +267,8 @@ class GoogleSignIn {
267267 return getAccessToken(context, GoogleSignIn .getLastSignedInAccount(context))
268268 }
269269
270+ private const val GET_TOKENS_ERROR = " getTokens requires a user to be signed in" ;
271+
270272 private fun getAccessToken (context : Context , account : GoogleSignInAccount ? ): String? {
271273 if (account?.account != null ) {
272274 return GoogleAuthUtil .getToken(
@@ -275,7 +277,7 @@ class GoogleSignIn {
275277 scopesToString(account.grantedScopes)
276278 )
277279 } else {
278- throw Exception (" getTokens requires a user to be signed in " )
280+ throw Exception (GET_TOKENS_ERROR )
279281 }
280282 }
281283
@@ -284,23 +286,30 @@ class GoogleSignIn {
284286 executors.submit {
285287 try {
286288 val account = GoogleSignIn .getLastSignedInAccount(context)
287- ? : throw Exception (" getTokens requires a user to be signed in" )
288- val token = getAccessToken(context)
289- val tokens = JSONObject ()
290- tokens.put(" idToken" , account.idToken)
291- tokens.put(" accessToken" , token)
292- runOnMain {
293- callback.onSuccess(tokens.toString())
289+ if (account != null ) {
290+ val token = getAccessToken(context)
291+ val tokens = JSONObject ()
292+ tokens.put(" idToken" , account.idToken)
293+ tokens.put(" accessToken" , token)
294+
295+ if (retrieveAccessToken) {
296+ accessToken = token
297+ }
298+
299+ runOnMain {
300+ callback.onSuccess(tokens.toString())
301+ }
302+ } else {
303+ callback.onError(Exception (GET_TOKENS_ERROR ))
294304 }
295305 } catch (e: Exception ) {
296306 runOnMain {
297- callback.onError(Exception (" getTokens requires a user to be signed in " ))
307+ callback.onError(Exception (GET_TOKENS_ERROR ))
298308 }
299309 }
300310 }
301311 }
302312
303-
304313 @JvmStatic
305314 fun getSignedInAccountFromIntent (intent : Intent , callback : Callback <GoogleUser >) {
306315 GoogleSignIn .getSignedInAccountFromIntent(intent)
0 commit comments