18
18
import java .io .IOException ;
19
19
import java .util .Map ;
20
20
import java .util .Set ;
21
+ import java .util .Iterator ;
21
22
import java .util .HashMap ;
22
23
import java .util .List ;
23
24
import java .util .ArrayList ;
30
31
import com .facebook .react .bridge .Callback ;
31
32
import com .facebook .react .bridge .WritableMap ;
32
33
import com .facebook .react .bridge .ReadableMap ;
34
+ import com .facebook .react .bridge .ReadableArray ;
33
35
import com .facebook .react .bridge .ReadableType ;
34
36
import com .facebook .react .bridge .ReadableMapKeySetIterator ;
35
37
import com .facebook .react .bridge .ReactContext ;
@@ -91,6 +93,7 @@ public void configureProvider(
91
93
String callbackUrlStr = params .getString ("callback_url" );
92
94
_callbackUrls .add (callbackUrlStr );
93
95
96
+ Log .d (TAG , "Added callback url " + callbackUrlStr + " for providler " + providerName );
94
97
95
98
// Keep configuration map
96
99
HashMap <String , Object > cfg = new HashMap <String ,Object >();
@@ -130,7 +133,10 @@ public void authorize(
130
133
String callbackUrl = "http://localhost/" + providerName ;
131
134
132
135
OAuthManagerOnAccessTokenListener listener = new OAuthManagerOnAccessTokenListener () {
133
- public void onRequestTokenError (final Exception ex ) {}
136
+ public void onRequestTokenError (final Exception ex ) {
137
+ Log .e (TAG , "Exception with request token: " + ex .getMessage ());
138
+ _credentialsStore .delete (providerName );
139
+ }
134
140
public void onOAuth1AccessToken (final OAuth1AccessToken accessToken ) {
135
141
_credentialsStore .store (providerName , accessToken );
136
142
_credentialsStore .commit ();
@@ -284,18 +290,23 @@ private OAuthRequest oauthRequestWithParams(
284
290
@ Nullable final ReadableMap params
285
291
) throws Exception {
286
292
OAuthRequest request ;
293
+ OAuthConfig config ;
294
+ OAuth1AccessToken oa1token = null ;
295
+ OAuth2AccessToken oa2token = null ;
287
296
288
297
if (authVersion .equals ("1.0" )) {
289
298
final OAuth10aService service =
290
299
OAuthManagerProviders .getApiFor10aProvider (providerName , cfg , null );
300
+ oa1token = _credentialsStore .get (providerName , OAuth1AccessToken .class );
291
301
292
- final OAuthConfig config = service .getConfig ();
302
+ config = service .getConfig ();
293
303
request = new OAuthRequest (httpVerb , url .toString (), config );
294
304
} else if (authVersion .equals ("2.0" )) {
295
305
final OAuth20Service service =
296
306
OAuthManagerProviders .getApiFor20Provider (providerName , cfg , null );
307
+ oa2token = _credentialsStore .get (providerName , OAuth2AccessToken .class );
297
308
298
- final OAuthConfig config = service .getConfig ();
309
+ config = service .getConfig ();
299
310
request = new OAuthRequest (httpVerb , url .toString (), config );
300
311
} else {
301
312
Log .e (TAG , "Error in making request method" );
@@ -311,6 +322,13 @@ private OAuthRequest oauthRequestWithParams(
311
322
case String :
312
323
String val = params .getString (key );
313
324
// String escapedVal = Uri.encode(val);
325
+ if (val .equals ("access_token" )) {
326
+ if (oa1token != null ) {
327
+ val = oa1token .toString ();
328
+ } else if (oa2token != null ) {
329
+ val = oa2token .toString ();
330
+ }
331
+ }
314
332
request .addParameter (key , val );
315
333
break ;
316
334
default :
@@ -449,7 +467,7 @@ private WritableMap accessTokenResponse(
449
467
450
468
return resp ;
451
469
}
452
-
470
+
453
471
454
472
private void exceptionCallback (Exception ex , final Callback onFail ) {
455
473
WritableMap error = Arguments .createMap ();
@@ -459,4 +477,71 @@ private void exceptionCallback(Exception ex, final Callback onFail) {
459
477
460
478
onFail .invoke (error );
461
479
}
462
- }
480
+
481
+ public static Map <String , Object > recursivelyDeconstructReadableMap (ReadableMap readableMap ) {
482
+ Map <String , Object > deconstructedMap = new HashMap <>();
483
+ if (readableMap == null ) {
484
+ return deconstructedMap ;
485
+ }
486
+
487
+ ReadableMapKeySetIterator iterator = readableMap .keySetIterator ();
488
+ while (iterator .hasNextKey ()) {
489
+ String key = iterator .nextKey ();
490
+ ReadableType type = readableMap .getType (key );
491
+ switch (type ) {
492
+ case Null :
493
+ deconstructedMap .put (key , null );
494
+ break ;
495
+ case Boolean :
496
+ deconstructedMap .put (key , readableMap .getBoolean (key ));
497
+ break ;
498
+ case Number :
499
+ deconstructedMap .put (key , readableMap .getDouble (key ));
500
+ break ;
501
+ case String :
502
+ deconstructedMap .put (key , readableMap .getString (key ));
503
+ break ;
504
+ case Map :
505
+ deconstructedMap .put (key , OAuthManagerModule .recursivelyDeconstructReadableMap (readableMap .getMap (key )));
506
+ break ;
507
+ case Array :
508
+ deconstructedMap .put (key , OAuthManagerModule .recursivelyDeconstructReadableArray (readableMap .getArray (key )));
509
+ break ;
510
+ default :
511
+ throw new IllegalArgumentException ("Could not convert object with key: " + key + "." );
512
+ }
513
+
514
+ }
515
+ return deconstructedMap ;
516
+ }
517
+
518
+ public static List <Object > recursivelyDeconstructReadableArray (ReadableArray readableArray ) {
519
+ List <Object > deconstructedList = new ArrayList <>(readableArray .size ());
520
+ for (int i = 0 ; i < readableArray .size (); i ++) {
521
+ ReadableType indexType = readableArray .getType (i );
522
+ switch (indexType ) {
523
+ case Null :
524
+ deconstructedList .add (i , null );
525
+ break ;
526
+ case Boolean :
527
+ deconstructedList .add (i , readableArray .getBoolean (i ));
528
+ break ;
529
+ case Number :
530
+ deconstructedList .add (i , readableArray .getDouble (i ));
531
+ break ;
532
+ case String :
533
+ deconstructedList .add (i , readableArray .getString (i ));
534
+ break ;
535
+ case Map :
536
+ deconstructedList .add (i , OAuthManagerModule .recursivelyDeconstructReadableMap (readableArray .getMap (i )));
537
+ break ;
538
+ case Array :
539
+ deconstructedList .add (i , OAuthManagerModule .recursivelyDeconstructReadableArray (readableArray .getArray (i )));
540
+ break ;
541
+ default :
542
+ throw new IllegalArgumentException ("Could not convert object at index " + i + "." );
543
+ }
544
+ }
545
+ return deconstructedList ;
546
+ }
547
+ }
0 commit comments