33import android .content .Context ;
44import android .net .Uri ;
55
6+ import com .codepath .asynchttpclient .AsyncHttpClient ;
67import com .codepath .utils .AsyncSimpleTask ;
78import com .github .scribejava .core .builder .ServiceBuilder ;
89import com .github .scribejava .core .builder .api .BaseApi ;
910import com .github .scribejava .core .exceptions .OAuthException ;
11+ import com .github .scribejava .core .model .OAuth1AccessToken ;
1012import com .github .scribejava .core .model .OAuth1RequestToken ;
13+ import com .github .scribejava .core .model .OAuth2AccessToken ;
14+ import com .github .scribejava .core .model .OAuthAsyncRequestCallback ;
1115import com .github .scribejava .core .model .OAuthConstants ;
1216import com .github .scribejava .core .model .Token ;
1317import com .github .scribejava .core .oauth .OAuth10aService ;
1418import com .github .scribejava .core .oauth .OAuth20Service ;
1519import com .github .scribejava .core .oauth .OAuthService ;
16- import com .loopj .android .http .AsyncHttpClient ;
17- import com .loopj .android .http .RequestHandle ;
18- import com .loopj .android .http .ResponseHandlerInterface ;
1920
2021/*
2122 * OAuthAsyncHttpClient is responsible for managing the request and access token exchanges and then
@@ -45,86 +46,85 @@ public OAuthAsyncHttpClient(BaseApi apiInstance, String consumerKey, String cons
4546 // Once fetched, fire the onReceivedRequestToken for the request token handler
4647 // Works for both OAuth1.0a and OAuth2
4748 public void fetchRequestToken () {
48- new AsyncSimpleTask (new AsyncSimpleTask .AsyncSimpleTaskHandler () {
49- String authorizeUrl = null ;
50- Exception e = null ;
51- Token requestToken ;
52-
53- public void doInBackground () {
54- try {
55- if (service .getVersion () == "1.0" ) {
56- OAuth10aService oAuth10aService = (OAuth10aService ) service ;
57- requestToken = oAuth10aService .getRequestToken ();
58- authorizeUrl = oAuth10aService .getAuthorizationUrl ((OAuth1RequestToken ) requestToken );
59- } else if (service .getVersion () == "2.0" ) {
60- OAuth20Service oAuth20Service = (OAuth20Service ) service ;
61- authorizeUrl = oAuth20Service .getAuthorizationUrl (null );
62- }
63- } catch (Exception e ) {
64- this .e = e ;
49+ if (service .getVersion () == "1.0" ) {
50+ final OAuth10aService oAuth10aService = (OAuth10aService ) service ;
51+ oAuth10aService .getRequestTokenAsync (new OAuthAsyncRequestCallback <OAuth1RequestToken >() {
52+ @ Override
53+ public void onCompleted (OAuth1RequestToken requestToken ) {
54+ String authorizeUrl = oAuth10aService .getAuthorizationUrl ((OAuth1RequestToken ) requestToken );
55+ handler .onReceivedRequestToken (requestToken , authorizeUrl , service .getVersion ());
56+
6557 }
66- }
6758
68- public void onPostExecute () {
69- if (e != null ) {
70- handler .onFailure (e );
71- } else {
72- handler .onReceivedRequestToken (requestToken , authorizeUrl , service .getVersion ());
59+ @ Override
60+ public void onThrowable (Throwable t ) {
61+ handler .onFailure (new Exception (t .getMessage ()));
7362 }
74- }
75- });
63+ });
64+ }
65+ if (service .getVersion () == "2.0" ) {
66+ OAuth20Service oAuth20Service = (OAuth20Service ) service ;
67+ String authorizeUrl = oAuth20Service .getAuthorizationUrl (null );
68+ handler .onReceivedRequestToken (null , authorizeUrl , service .getVersion ());
69+ }
7670 }
7771
7872 // Get the access token by exchanging the requestToken to the defined URL
7973 // Once receiving the access token, fires the onReceivedAccessToken method on the handler
8074 public void fetchAccessToken (final Token requestToken , final Uri uri ) {
8175
82- new AsyncSimpleTask (new AsyncSimpleTask .AsyncSimpleTaskHandler () {
83- Exception e = null ;
84-
85- public void doInBackground () {
86- // Fetch the verifier code from redirect url parameters
87- Uri authorizedUri = uri ;
88-
89- try {
90- if (service .getVersion () == "1.0" ) {
91- // Use verifier token to fetch access token
92-
93- if (authorizedUri .getQuery ().contains (OAuthConstants .VERIFIER )) {
94- String oauth_verifier = authorizedUri .getQueryParameter (OAuthConstants .VERIFIER );
95- OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken ) requestToken ;
96- OAuth10aService oAuth10aService = (OAuth10aService ) service ;
97- accessToken = oAuth10aService .getAccessToken (oAuth1RequestToken , oauth_verifier );
98- }
99- else { // verifier was null
100- throw new OAuthException ("No verifier code was returned with uri '" + uri + "' " +
101- "and access token cannot be retrieved" );
102- }
103- } else if (service .getVersion () == "2.0" ) {
104- if (authorizedUri .getQuery ().contains (OAuthConstants .CODE )) {
105- String code = authorizedUri .getQueryParameter (OAuthConstants .CODE );
106- OAuth20Service oAuth20Service = (OAuth20Service ) service ;
107- accessToken = oAuth20Service .getAccessToken (code );
108- }
109- else { // verifier was null
110- throw new OAuthException ("No code was returned with uri '" + uri + "' " +
111- "and access token cannot be retrieved" );
112- }
113- }
114- } catch (Exception e ) {
115- this .e = e ;
116- }
76+ Uri authorizedUri = uri ;
77+
78+ if (service .getVersion () == "1.0" ) {
79+ // Use verifier token to fetch access token
80+
81+ if (authorizedUri .getQuery ().contains (OAuthConstants .VERIFIER )) {
82+ String oauth_verifier = authorizedUri .getQueryParameter (OAuthConstants .VERIFIER );
83+ OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken ) requestToken ;
84+ OAuth10aService oAuth10aService = (OAuth10aService ) service ;
85+
86+ oAuth10aService .getAccessTokenAsync (oAuth1RequestToken , oauth_verifier ,
87+ new OAuthAsyncRequestCallback <OAuth1AccessToken >() {
88+
89+ @ Override
90+ public void onCompleted (OAuth1AccessToken oAuth1AccessToken ) {
91+ handler .onReceivedAccessToken (oAuth1AccessToken , service .getVersion ());
92+ }
93+
94+ @ Override
95+ public void onThrowable (Throwable e ) {
96+ handler .onFailure (new OAuthException (e .getMessage ()));
97+ }
98+ });
99+
117100 }
101+ else { // verifier was null
102+ throw new OAuthException ("No verifier code was returned with uri '" + uri + "' " +
103+ "and access token cannot be retrieved" );
104+ }
105+ } else if (service .getVersion () == "2.0" ) {
106+ if (authorizedUri .getQuery ().contains (OAuthConstants .CODE )) {
107+ String code = authorizedUri .getQueryParameter (OAuthConstants .CODE );
108+ OAuth20Service oAuth20Service = (OAuth20Service ) service ;
109+ oAuth20Service .getAccessToken (code , new OAuthAsyncRequestCallback <OAuth2AccessToken >() {
110+ @ Override
111+ public void onCompleted (OAuth2AccessToken accessToken ) {
112+ setAccessToken (accessToken );
113+ handler .onReceivedAccessToken (accessToken , service .getVersion ());
118114
119- public void onPostExecute () {
120- if ( e != null ) {
121- handler . onFailure ( e );
122- } else {
123- setAccessToken ( accessToken );
124- handler . onReceivedAccessToken ( accessToken , service . getVersion ());
125- }
115+ }
116+
117+ @ Override
118+ public void onThrowable ( Throwable t ) {
119+
120+ }
121+ });
126122 }
127- });
123+ else { // verifier was null
124+ handler .onFailure (new OAuthException ("No code was returned with uri '" + uri + "' " +
125+ "and access token cannot be retrieved" ));
126+ }
127+ }
128128 }
129129
130130 // Set the access token used for signing requests
@@ -133,40 +133,13 @@ public void setAccessToken(Token accessToken) {
133133 this .accessToken = null ;
134134 } else {
135135 this .accessToken = accessToken ;
136- }
136+ }
137137 }
138138
139139 public Token getAccessToken () {
140140 return this .accessToken ;
141141 }
142-
143- // Send scribe signed request based on the async http client to construct a signed request
144- // Accepts an HttpEntity which has the underlying entity for the request params
145-
146- @ Override
147- protected RequestHandle sendRequest (
148- cz .msebera .android .httpclient .impl .client .DefaultHttpClient client ,
149- cz .msebera .android .httpclient .protocol .HttpContext httpContext ,
150- cz .msebera .android .httpclient .client .methods .HttpUriRequest uriRequest ,
151- String contentType , ResponseHandlerInterface responseHandler ,
152- Context context ) {
153-
154- if (this .service != null && accessToken != null ) {
155- try {
156- ScribeRequestAdapter adapter = new ScribeRequestAdapter (uriRequest );
157- this .service .signRequest (accessToken , adapter );
158- return super .sendRequest (client , httpContext , uriRequest , contentType , responseHandler , context );
159- } catch (Exception e ) {
160- e .printStackTrace ();
161- }
162- } else if (accessToken == null ) {
163- throw new OAuthException ("Cannot send unauthenticated requests for " + apiInstance .getClass ().getSimpleName () + " client. Please attach an access token!" );
164- } else { // service is null
165- throw new OAuthException ("Cannot send unauthenticated requests for undefined service. Please specify a valid api service!" );
166- }
167- return null ; // Hopefully never reaches here
168- }
169-
142+
170143 // Defines the interface handler for different token handlers
171144 public interface OAuthTokenHandler {
172145 public void onReceivedRequestToken (Token requestToken , String authorizeUrl , String oAuthVersion );
0 commit comments