protected void acquireAccessToken ( final OAuthCallback cb ) { if ( isAccessTokenCached ( ) ) { // Execute the callback immediately with cached OAuth credentials if ( VERBOSE ) Log . d ( TAG , "Access token cached" ) ; if ( cb != null ) { // Ensure networking occurs off the main thread // TODO: Use an ExecutorService and expose an application shutdown method to shutdown? new Thread ( new Runnable ( ) { @ Override public void run ( ) { cb . onSuccess ( getRequestFactoryFromCachedCredentials ( ) ) ; } } ) . start ( ) ; } } else if ( mOauthInProgress && cb != null ) { // Add the callback to the queue for execution when outstanding OAuth negotiation complete mCallbackQueue . add ( cb ) ; if ( VERBOSE ) Log . i ( TAG , "Adding cb to queue" ) ; } else { mOauthInProgress = true ; // Perform an OAuth Client Credentials Request // TODO: Replace with new Thread() new AsyncTask < Void , Void , Void > ( ) { @ Override protected Void doInBackground ( Void ... params ) { TokenResponse response = null ; try { if ( VERBOSE ) Log . i ( TAG , "Fetching OAuth " + mConfig . getAccessTokenRequestUrl ( ) ) ; response = new ClientCredentialsTokenRequest ( new NetHttpTransport ( ) , new JacksonFactory ( ) , new GenericUrl ( mConfig . getAccessTokenRequestUrl ( ) ) ) . setGrantType ( "client_credentials" ) . setClientAuthentication ( new BasicAuthentication ( mConfig . getClientId ( ) , mConfig . getClientSecret ( ) ) ) . execute ( ) ; } catch ( IOException e ) { // TODO: Alert user Kickflip down // or client credentials invalid if ( cb != null ) { postExceptionToCallback ( cb , e ) ; } e . printStackTrace ( ) ; } if ( response != null ) { if ( VERBOSE ) Log . i ( TAG , "Got Access Token " + response . getAccessToken ( ) . substring ( 0 , 5 ) + "..." ) ; storeAccessToken ( response ) ; mOauthInProgress = false ; if ( cb != null ) cb . onSuccess ( getRequestFactoryFromAccessToken ( mStorage . getString ( ACCESS_TOKEN_KEY , null ) ) ) ; executeQueuedCallbacks ( ) ; } else { mOauthInProgress = false ; Log . w ( TAG , "Failed to get Access Token" ) ; } return null ; } } . execute ( ) ; } }
Asynchronously attempt to acquire an OAuth Access Token