8
8
import android .text .TextUtils ;
9
9
import im .delight .android .webview .AdvancedWebView ;
10
10
11
+ import android .net .Uri ;
11
12
import android .util .Log ;
12
13
import android .view .View ;
13
14
import android .view .ViewGroup ;
18
19
19
20
import com .github .scribejava .core .model .OAuth1RequestToken ;
20
21
import com .github .scribejava .core .model .OAuth1AccessToken ;
22
+ import com .github .scribejava .core .model .OAuth2AccessToken ;
23
+ import com .github .scribejava .core .model .Token ;
21
24
22
25
import com .github .scribejava .core .model .OAuthRequest ;
23
26
import com .github .scribejava .core .oauth .OAuthService ;
24
27
import com .github .scribejava .core .oauth .OAuth10aService ;
28
+ import com .github .scribejava .core .oauth .OAuth20Service ;
25
29
import com .github .scribejava .core .exceptions .OAuthConnectionException ;
26
30
27
31
// Credit where credit is due:
@@ -36,8 +40,10 @@ public class OAuthManagerFragmentController {
36
40
37
41
private String authVersion ;
38
42
private OAuth10aService oauth10aService ;
43
+ private OAuth20Service oauth20Service ;
39
44
private String callbackUrl ;
40
45
private OAuth1RequestToken oauth1RequestToken ;
46
+
41
47
private Runnable onAccessToken ;
42
48
private OAuthManagerOnAccessTokenListener mListener ;
43
49
@@ -59,6 +65,21 @@ public OAuthManagerFragmentController(
59
65
this .callbackUrl = callbackUrl ;
60
66
}
61
67
68
+ public OAuthManagerFragmentController (
69
+ android .app .FragmentManager fragmentManager ,
70
+ final String providerName ,
71
+ OAuth20Service oauthService ,
72
+ final String callbackUrl
73
+ ) {
74
+ this .uiHandler = new Handler (Looper .getMainLooper ());
75
+ this .fragmentManager = fragmentManager ;
76
+
77
+ this .authVersion = "2.0" ;
78
+ this .oauth20Service = oauthService ;
79
+ this .callbackUrl = callbackUrl ;
80
+ }
81
+
82
+
62
83
public void requestAuth (OAuthManagerOnAccessTokenListener listener ) {
63
84
mListener = listener ;
64
85
@@ -116,12 +137,13 @@ public void loaded10aAccessToken(final OAuth1AccessToken accessToken) {
116
137
Log .d (TAG , "Loaded access token in OAuthManagerFragmentController" );
117
138
Log .d (TAG , "AccessToken: " + accessToken + " (raw: " + accessToken .getRawResponse () + ")" );
118
139
119
- if (authVersion .equals ("1.0" )) {
120
- this .dismissDialog ();
121
- mListener .onOauth1AccessToken (accessToken );
122
- } else {
140
+ this .dismissDialog ();
141
+ mListener .onOAuth1AccessToken (accessToken );
142
+ }
123
143
124
- }
144
+ public void loaded20AccessToken (final OAuth2AccessToken accessToken ) {
145
+ this .dismissDialog ();
146
+ mListener .onOAuth2AccessToken (accessToken );
125
147
}
126
148
127
149
public void onError () {
@@ -135,11 +157,21 @@ public void getRequestTokenUrlAndLoad(AdvancedWebView webView) {
135
157
136
158
public void getAccessToken (
137
159
final AdvancedWebView webView ,
138
- final String oauthVerifier
160
+ final String url
139
161
) {
140
- Load10aAccessTokenTask task = new Load10aAccessTokenTask (
141
- this , webView , oauth1RequestToken , oauthVerifier );
142
- task .execute ();
162
+ Uri responseUri = Uri .parse (url );
163
+ if (authVersion .equals ("1.0" )) {
164
+ String oauthToken = responseUri .getQueryParameter ("oauth_token" );
165
+ String oauthVerifier = responseUri .getQueryParameter ("oauth_verifier" );
166
+ Load1AccessTokenTask task = new Load1AccessTokenTask (
167
+ this , webView , oauth1RequestToken , oauthVerifier );
168
+ task .execute ();
169
+ } else if (authVersion .equals ("2.0" )) {
170
+ String code = responseUri .getQueryParameter ("code" );
171
+ Load2AccessTokenTask task = new Load2AccessTokenTask (
172
+ this , webView , code );
173
+ task .execute ();
174
+ }
143
175
}
144
176
145
177
////// TASKS
@@ -185,6 +217,10 @@ protected String doInBackground(Void... params) {
185
217
final String requestTokenUrl =
186
218
oauth10aService .getAuthorizationUrl (oauth1RequestToken );
187
219
return requestTokenUrl ;
220
+ } else if (authVersion .equals ("2.0" )) {
221
+ final String authorizationUrl =
222
+ oauth20Service .getAuthorizationUrl ();
223
+ return authorizationUrl ;
188
224
} else {
189
225
return null ;
190
226
}
@@ -204,21 +240,25 @@ protected void onPostExecute(final String url) {
204
240
runOnMainThread (new Runnable () {
205
241
@ Override
206
242
public void run () {
207
- if (url != null ) {
243
+ if (url == null ) {
244
+ mCtrl .onError ();
245
+ return ;
246
+ }
247
+ if (authVersion .equals ("1.0" )) {
208
248
mCtrl .setRequestToken (oauth1RequestToken );
209
249
mWebView .loadUrl (url );
210
- } else {
211
- mCtrl . onError ( );
250
+ } else if ( authVersion . equals ( "2.0" )) {
251
+ mWebView . loadUrl ( url );
212
252
}
213
253
}
214
254
});
215
255
}
216
256
}
217
257
218
- private class Load10aAccessTokenTask extends OAuthTokenTask <OAuth1AccessToken > {
258
+ private class Load1AccessTokenTask extends OAuthTokenTask <OAuth1AccessToken > {
219
259
private String oauthVerifier ;
220
260
221
- public Load10aAccessTokenTask (
261
+ public Load1AccessTokenTask (
222
262
OAuthManagerFragmentController ctrl ,
223
263
AdvancedWebView view ,
224
264
OAuth1RequestToken requestToken ,
@@ -231,13 +271,9 @@ public Load10aAccessTokenTask(
231
271
@ Override
232
272
protected OAuth1AccessToken doInBackground (Void ... params ) {
233
273
try {
234
- if (authVersion .equals ("1.0" )) {
235
- final OAuth1AccessToken accessToken =
236
- oauth10aService .getAccessToken (oauth1RequestToken , oauthVerifier );
237
- return accessToken ;
238
- } else {
239
- return null ;
240
- }
274
+ final OAuth1AccessToken accessToken =
275
+ (OAuth1AccessToken ) oauth10aService .getAccessToken (oauth1RequestToken , oauthVerifier );
276
+ return accessToken ;
241
277
} catch (OAuthConnectionException ex ) {
242
278
Log .e (TAG , "OAuth connection exception: " + ex .getMessage ());
243
279
ex .printStackTrace ();
@@ -254,11 +290,55 @@ protected void onPostExecute(final OAuth1AccessToken accessToken) {
254
290
runOnMainThread (new Runnable () {
255
291
@ Override
256
292
public void run () {
257
- if (accessToken != null ) {
258
- mCtrl .loaded10aAccessToken (accessToken );
259
- } else {
293
+ if (accessToken == null ) {
294
+ mCtrl .onError ();
295
+ return ;
296
+ }
297
+ mCtrl .loaded10aAccessToken (accessToken );
298
+ }
299
+ });
300
+ }
301
+ }
302
+
303
+ private class Load2AccessTokenTask extends OAuthTokenTask <OAuth2AccessToken > {
304
+ private String authorizationCode ;
305
+
306
+ public Load2AccessTokenTask (
307
+ OAuthManagerFragmentController ctrl ,
308
+ AdvancedWebView view ,
309
+ String authorizationCode
310
+ ) {
311
+ super (ctrl , view );
312
+ this .authorizationCode = authorizationCode ;
313
+ }
314
+
315
+ @ Override
316
+ protected OAuth2AccessToken doInBackground (Void ... params ) {
317
+ try {
318
+ final OAuth2AccessToken accessToken =
319
+ (OAuth2AccessToken ) oauth20Service .getAccessToken (authorizationCode );
320
+ return accessToken ;
321
+ } catch (OAuthConnectionException ex ) {
322
+ Log .e (TAG , "OAuth connection exception: " + ex .getMessage ());
323
+ ex .printStackTrace ();
324
+ return null ;
325
+ } catch (IOException ex ) {
326
+ Log .e (TAG , "An exception occurred getRequestToken: " + ex .getMessage ());
327
+ ex .printStackTrace ();
328
+ return null ;
329
+ }
330
+ }
331
+
332
+ @ Override
333
+ protected void onPostExecute (final OAuth2AccessToken accessToken ) {
334
+ runOnMainThread (new Runnable () {
335
+ @ Override
336
+ public void run () {
337
+ if (accessToken == null ) {
260
338
mCtrl .onError ();
339
+ return ;
261
340
}
341
+ mCtrl .loaded20AccessToken (accessToken );
262
342
}
263
343
});
264
344
}
0 commit comments