1010
1111import android .app .ProgressDialog ;
1212import android .content .Context ;
13- import android .net .Uri ;
1413import android .os .AsyncTask ;
1514import android .webkit .CookieSyncManager ;
1615
1716import com .parse .oauth .OAuth1FlowDialog ;
1817import com .parse .oauth .OAuth1FlowDialog .FlowResultHandler ;
1918import com .parse .oauth .OAuth1FlowException ;
2019
21- import java .net .HttpURLConnection ;
22-
23- import oauth .signpost .OAuthConsumer ;
24- import oauth .signpost .OAuthProvider ;
25- import oauth .signpost .basic .DefaultOAuthConsumer ;
26- import oauth .signpost .basic .DefaultOAuthProvider ;
2720import oauth .signpost .http .HttpParameters ;
21+ import okhttp3 .HttpUrl ;
22+ import se .akerfeldt .okhttp .signpost .OkHttpOAuthConsumer ;
23+ import se .akerfeldt .okhttp .signpost .OkHttpOAuthProvider ;
2824
2925public class Twitter {
30- private static final String USER_AGENT = "Parse Android SDK" ;
3126
32- private static final String REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token" ;
33- private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authenticate" ;
34- private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token" ;
27+ static final String REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token" ;
28+ static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authenticate" ;
29+ static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token" ;
3530
3631 private static final String VERIFIER_PARAM = "oauth_verifier" ;
3732 private static final String USER_ID_PARAM = "user_id" ;
@@ -104,32 +99,37 @@ public void setScreenName(String screenName) {
10499 this .screenName = screenName ;
105100 }
106101
107- public void signRequest (HttpURLConnection request ) {
108- OAuthConsumer consumer = new DefaultOAuthConsumer (getConsumerKey (), getConsumerSecret ());
109- consumer .setTokenWithSecret (getAuthToken (), getAuthTokenSecret ());
110- try {
111- consumer .sign (request );
112- } catch (Exception e ) {
113- throw new RuntimeException (e );
114- }
115- }
116-
117102 public void authorize (final Context context , final AsyncCallback callback ) {
118103 if (getConsumerKey () == null || getConsumerKey ().length () == 0 || getConsumerSecret () == null
119104 || getConsumerSecret ().length () == 0 ) {
120105 throw new IllegalStateException (
121106 "Twitter must be initialized with a consumer key and secret before authorization." );
122107 }
123108
124- final OAuthProvider provider = new DefaultOAuthProvider (REQUEST_TOKEN_URL , ACCESS_TOKEN_URL , AUTHORIZE_URL );
125- provider .setRequestHeader ("User-Agent" , USER_AGENT );
126- final OAuthConsumer consumer = new DefaultOAuthConsumer (getConsumerKey (), getConsumerSecret ());
109+ final OkHttpOAuthProvider provider = new OkHttpOAuthProvider (REQUEST_TOKEN_URL , ACCESS_TOKEN_URL , AUTHORIZE_URL );
110+ final OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer (getConsumerKey (), getConsumerSecret ());
127111
128112 final ProgressDialog progress = new ProgressDialog (context );
129113 progress .setMessage ("Loading..." );
130114 AsyncTask <Void , Void , String > task = new AsyncTask <Void , Void , String >() {
131115 private Throwable error ;
132116
117+ @ Override
118+ protected void onPreExecute () {
119+ super .onPreExecute ();
120+ progress .show ();
121+ }
122+
123+ @ Override
124+ protected String doInBackground (Void ... params ) {
125+ try {
126+ return provider .retrieveRequestToken (consumer , callbackUrl );
127+ } catch (Throwable e ) {
128+ error = e ;
129+ }
130+ return null ;
131+ }
132+
133133 @ Override
134134 protected void onPostExecute (String result ) {
135135 super .onPostExecute (result );
@@ -150,8 +150,8 @@ public void onError(int errorCode, String description, String failingUrl) {
150150 @ Override
151151 public void onComplete (String callbackUrl ) {
152152 CookieSyncManager .getInstance ().sync ();
153- Uri uri = Uri .parse (callbackUrl );
154- final String verifier = uri . getQueryParameter (VERIFIER_PARAM );
153+ HttpUrl url = HttpUrl .parse (callbackUrl );
154+ final String verifier = url . queryParameter (VERIFIER_PARAM );
155155 if (verifier == null ) {
156156 callback .onCancel ();
157157 return ;
@@ -211,22 +211,6 @@ public void onCancel() {
211211 progress .dismiss ();
212212 }
213213 }
214-
215- @ Override
216- protected void onPreExecute () {
217- super .onPreExecute ();
218- progress .show ();
219- }
220-
221- @ Override
222- protected String doInBackground (Void ... params ) {
223- try {
224- return provider .retrieveRequestToken (consumer , callbackUrl );
225- } catch (Throwable e ) {
226- error = e ;
227- }
228- return null ;
229- }
230214 };
231215 task .execute ();
232216 }
0 commit comments