Skip to content

Commit 78ffea9

Browse files
committed
makeRequest for Android
1 parent b477fcd commit 78ffea9

File tree

3 files changed

+90
-28
lines changed

3 files changed

+90
-28
lines changed

android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.facebook.react.bridge.ReactContext;
3636

3737
import com.github.scribejava.core.builder.api.BaseApi;
38+
import com.github.scribejava.core.model.Verb;
3839

3940
// import com.wuman.android.auth.AuthorizationDialogController;
4041
// import com.wuman.android.auth.AuthorizationFlow;
@@ -191,31 +192,81 @@ public void makeRequest(
191192
final Callback onComplete) {
192193

193194
Log.i(TAG, "makeRequest called for " + providerName + " to " + urlString);
194-
// try {
195-
// Credential creds = this.loadCredentialForProvider(providerName, params);
196-
// HashMap<String,String> cfg = this.getConfiguration(providerName);
197-
// final String authVersion = (String) cfg.get("auth_version");
198-
199-
// URL url;
200-
// try {
201-
// if (urlString.contains("http")) {
202-
// url = new URL(urlString);
203-
// } else {
204-
// String apiHost = (String) cfg.get("api_url");
205-
// url = new URL(apiHost + urlString);
206-
// }
207-
// } catch (MalformedURLException ex) {
208-
// Log.e(TAG, "Bad url. Check request and try again: " + ex.getMessage());
209-
// exceptionCallback(ex, onComplete);
210-
// return;
211-
// }
212-
195+
try {
196+
HashMap<String,String> cfg = this.getConfiguration(providerName);
197+
final String authVersion = (String) cfg.get("auth_version");
198+
199+
URL url;
200+
try {
201+
if (urlString.contains("http")) {
202+
url = new URL(urlString);
203+
} else {
204+
String apiHost = (String) cfg.get("api_url");
205+
url = new URL(apiHost + urlString);
206+
}
207+
} catch (MalformedURLException ex) {
208+
Log.e(TAG, "Bad url. Check request and try again: " + ex.getMessage());
209+
exceptionCallback(ex, onComplete);
210+
return;
211+
}
212+
213+
String httpMethod;
214+
if (params.hasKey("method")) {
215+
httpMethod = params.getString("method");
216+
} else {
217+
httpMethod = "GET";
218+
}
219+
220+
Verb httpVerb;
221+
if (httpMethod.equalsIgnoreCase("GET")) {
222+
httpVerb = Verb.GET;
223+
} else if (httpMethod.equalsIgnoreCase("POST")) {
224+
httpVerb = Verb.POST;
225+
} else if (httpMethod.equalsIgnoreCase("PUT")) {
226+
httpVerb = Verb.PUT;
227+
} else if (httpMethod.equalsIgnoreCase("DELETE")) {
228+
httpVerb = Verb.DELETE;
229+
} else if (httpMethod.equalsIgnoreCase("OPTIONS")) {
230+
httpVerb = Verb.OPTIONS;
231+
} else if (httpMethod.equalsIgnoreCase("HEAD")) {
232+
httpVerb = Verb.HEAD;
233+
} else if (httpMethod.equalsIgnoreCase("PATCH")) {
234+
httpVerb = Verb.PATCH;
235+
} else if (httpMethod.equalsIgnoreCase("TRACE")) {
236+
httpVerb = Verb.TRACE;
237+
} else {
238+
httpVerb = Verb.GET;
239+
}
213240

241+
if (authVersion.equals("1.0")) {
242+
final OAuth10aService service =
243+
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null);
244+
OAuth1AccessToken token = _credentialsStore.get(providerName, OAuth1AccessToken.class);
245+
final OAuthRequest request = new OAuthRequest(Verb.GET, url.toString(), service);
246+
247+
service.signRequest(token, request);
248+
final Response response = request.send();
249+
final String rawBody = response.getBody();
250+
251+
Log.d(TAG, "rawBody: " + rawBody);
252+
// final Object response = new Gson().fromJson(rawBody, Object.class);
253+
254+
WritableMap resp = Arguments.createMap();
255+
resp.putInt("status", response.getCode());
256+
resp.putString("data", rawBody);
257+
onComplete.invoke(null, resp);
258+
} else {
259+
// TODO:
260+
}
214261

215-
// } catch (Exception ex) {
216-
// Log.e(TAG, "Exception when making request: " + ex.getMessage());
217-
// exceptionCallback(ex, onComplete);
218-
// }
262+
} catch (IOException ex) {
263+
Log.e(TAG, "IOException when making request: " + ex.getMessage());
264+
ex.printStackTrace();
265+
exceptionCallback(ex, onComplete);
266+
} catch (Exception ex) {
267+
Log.e(TAG, "Exception when making request: " + ex.getMessage());
268+
exceptionCallback(ex, onComplete);
269+
}
219270
}
220271

221272
@ReactMethod

android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ private static OAuth10aService twitterService(final HashMap cfg, final String ca
3535
String consumerKey = (String) cfg.get("consumer_key");
3636
String consumerSecret = (String) cfg.get("consumer_secret");
3737

38-
return new ServiceBuilder()
38+
ServiceBuilder builder = new ServiceBuilder()
3939
.apiKey(consumerKey)
4040
.apiSecret(consumerSecret)
41-
.callback(callbackUrl)
42-
.debug()
43-
.build(TwitterApi.instance());
41+
.debug();
42+
43+
if (callbackUrl != null) {
44+
builder.callback(callbackUrl);
45+
}
46+
return builder.build(TwitterApi.instance());
4447
}
4548
}

react-native-oauth.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ export default class OAuthManager {
6767
app_name: this.appName
6868
});
6969

70-
return promisify('makeRequest')(provider, url, options);
70+
return promisify('makeRequest')(provider, url, options)
71+
.then(response => {
72+
// Little bit of a hack to support Android until we have a better
73+
// way of decoding the JSON response on the Android side
74+
if (response && response.data && typeof response.data === "string") {
75+
response.data = JSON.parse(response.data);
76+
}
77+
return response;
78+
});
7179
}
7280

7381
deauthorize(provider) {

0 commit comments

Comments
 (0)