diff --git a/.gitignore b/.gitignore index 04e5cbb..456e9ac 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,11 @@ local.properties .cproject # PDT-specific -.buildpath \ No newline at end of file +.buildpath + +# Android Studio project files +*.iml +.gradle +.idea +build +import-summary.txt \ No newline at end of file diff --git a/README.md b/README.md index c87f42b..bece151 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ This library leverages a few key libraries underneath to power the functionality You first need to make sure to download the prerequisites for using this library: - * [scribe-codepath.jar](https://www.dropbox.com/s/2ocu8cexujaustg/scribe-codepath.jar) - * [codepath-utils.jar](https://www.dropbox.com/s/6y5elx9dxjrcxim/codepath-utils.jar) - * [android-async-http-client.jar](https://www.dropbox.com/s/9ez0ts8dwuohprk/android-async-http-1.4.3.jar) + * [scribe-codepath.jar](https://www.dropbox.com/s/h7risvofhpejxcb/scribe-codepath-0.0.3.jar?dl=1) + * [codepath-utils.jar](https://www.dropbox.com/s/6y5elx9dxjrcxim/codepath-utils.jar?dl=1) + * [android-async-http-client.jar](https://www.dropbox.com/s/2e9l3kqvaf7h8xe/android-async-http-1.4.6.jar?dl=1) -Next download the [codepath-oauth.jar](https://www.dropbox.com/s/2lyeq2by1u01jki/codepath-oauth-0.2.4.jar) file. +Next download the [codepath-oauth.jar](https://www.dropbox.com/s/64wiil6ngzoomqx/codepath-oauth-0.4.1.jar?dl=1) file. Move all of these jars into the "libs" folder of the desired Android project. If you want an easier way to get setup with this library, try downloading the @@ -135,23 +135,23 @@ with a `JsonHttpResponseHandler` handler: // SomeActivity.java RestClient client = RestClientApp.getRestClient(); client.getHomeTimeline(1, new JsonHttpResponseHandler() { - public void onSuccess(JSONArray json) { + public void onSuccess(int statusCode, Header[] headers, JSONArray json) { // Response is automatically parsed into a JSONArray // json.getJSONObject(0).getLong("id"); } }); ``` -Based on the JSON response (array or object), you need to declare the expected type inside the `onSuccess` signature i.e `public void onSuccess(JSONObject json)`. If the endpoint does not return JSON, then you can use the `AsyncHttpResponseHandler`: +Based on the JSON response (array or object), you need to declare the expected type inside the `onSuccess` signature i.e `public void onSuccess(int statusCode, Header[] headers, JSONObject json)`. If the endpoint does not return JSON, then you can use the `AsyncHttpResponseHandler`: ```java RestClient client = RestClientApp.getRestClient(); client.get("http://www.google.com", new AsyncHttpResponseHandler() { @Override - public void onSuccess(String response) { + public void onSuccess(int statusCode, Header[] headers, String response) { System.out.println(response); } }); ``` -Check out [Android Async HTTP Docs](http://loopj.com/android-async-http/) for more request creation details. +Check out [Android Async HTTP Docs](http://loopj.com/android-async-http/) for more request creation details. \ No newline at end of file diff --git a/codepath-oauth.jardesc b/codepath-oauth.jardesc index 26dae0c..a7d097e 100644 --- a/codepath-oauth.jardesc +++ b/codepath-oauth.jardesc @@ -1,6 +1,6 @@ - + - + diff --git a/libs/android-async-http-1.4.3.jar b/libs/android-async-http-1.4.3.jar deleted file mode 100644 index 3a749a5..0000000 Binary files a/libs/android-async-http-1.4.3.jar and /dev/null differ diff --git a/libs/android-async-http-1.4.6.jar b/libs/android-async-http-1.4.6.jar new file mode 100644 index 0000000..70391cb Binary files /dev/null and b/libs/android-async-http-1.4.6.jar differ diff --git a/libs/android-support-v4.jar b/libs/android-support-v4.jar index c31cede..ab68dfa 100644 Binary files a/libs/android-support-v4.jar and b/libs/android-support-v4.jar differ diff --git a/libs/scribe-codepath-0.0.3.jar b/libs/scribe-codepath-0.0.3.jar new file mode 100644 index 0000000..ab752a4 Binary files /dev/null and b/libs/scribe-codepath-0.0.3.jar differ diff --git a/libs/scribe-codepath.jar b/libs/scribe-codepath.jar deleted file mode 100644 index c52e4d8..0000000 Binary files a/libs/scribe-codepath.jar and /dev/null differ diff --git a/src/com/codepath/oauth/OAuthAsyncHttpClient.java b/src/com/codepath/oauth/OAuthAsyncHttpClient.java index 6b7ab35..c474460 100644 --- a/src/com/codepath/oauth/OAuthAsyncHttpClient.java +++ b/src/com/codepath/oauth/OAuthAsyncHttpClient.java @@ -16,7 +16,8 @@ import com.codepath.utils.AsyncSimpleTask; import com.loopj.android.http.AsyncHttpClient; -import com.loopj.android.http.AsyncHttpResponseHandler; +import com.loopj.android.http.RequestHandle; +import com.loopj.android.http.ResponseHandlerInterface; /* * OAuthAsyncHttpClient is responsible for managing the request and access token exchanges and then @@ -130,20 +131,23 @@ public Token getAccessToken() { // Send scribe signed request based on the async http client to construct a signed request // Accepts an HttpEntity which has the underlying entity for the request params - protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, - String contentType, AsyncHttpResponseHandler responseHandler, Context context) { + @Override + protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, + String contentType, ResponseHandlerInterface responseHandler, Context context) { if (this.service != null && accessToken != null) { try { ScribeRequestAdapter adapter = new ScribeRequestAdapter(uriRequest); this.service.signRequest(accessToken, adapter); - super.sendRequest(client, httpContext, uriRequest, contentType, responseHandler, context); + return super.sendRequest(client, httpContext, uriRequest, contentType, responseHandler, context); } catch (Exception e) { e.printStackTrace(); } } else if (accessToken == null) { throw new OAuthException("Cannot send unauthenticated requests for " + apiClass.getSimpleName() + " client. Please attach an access token!"); + } else { // service is null + throw new OAuthException("Cannot send unauthenticated requests for undefined service. Please specify a valid api service!"); } - + return null; // Hopefully never reaches here } // Defines the interface handler for different token handlers diff --git a/src/com/codepath/oauth/OAuthLoginActionBarActivity.java b/src/com/codepath/oauth/OAuthLoginActionBarActivity.java new file mode 100644 index 0000000..501de66 --- /dev/null +++ b/src/com/codepath/oauth/OAuthLoginActionBarActivity.java @@ -0,0 +1,64 @@ +package com.codepath.oauth; + +import android.content.Intent; +import android.net.Uri; +import android.support.v7.app.ActionBarActivity; + +import com.codepath.utils.GenericsUtil; + +// This is the ActionBarActivity supportv7 version of LoginActivity +public abstract class OAuthLoginActionBarActivity extends ActionBarActivity + implements OAuthBaseClient.OAuthAccessHandler { + + private T client; + + // Use this to properly assign the new intent with callback code + // for activities with a "singleTask" launch mode + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + setIntent(intent); + } + + // Extract the uri data and call authorize to retrieve access token + // This is why after the browser redirects to the app, authentication is completed + @SuppressWarnings("unchecked") + @Override + protected void onResume() { + super.onResume(); + Class clientClass = getClientClass(); + // Extracts the authenticated url data after the user + // authorizes the OAuth app in the browser + Uri uri = getIntent().getData(); + + try { + client = (T) OAuthBaseClient.getInstance(clientClass, this); + client.authorize(uri, this); // fetch access token (if needed) + } catch (Exception e) { + e.printStackTrace(); + } + } + + public T getClient() { + return client; + } + + @SuppressWarnings("unchecked") + private Class getClientClass() { + return (Class) GenericsUtil.getTypeArguments(OAuthLoginActionBarActivity.class, this.getClass()).get(0); + } +} + +/* + * 1) Subclass OAuthBaseClient like TwitterClient + * 2) Subclass OAuthLoginActivity + * 3) Invoke .login + * 4) Optionally override + * a) onLoginSuccess + * b) onLoginFailure(Exception e) + * 5) In other activities that need the client + * a) c = TwitterClient.getSharedClient() +* b) c.getTimeline(...) + * 6) Modify AndroidManifest.xml to add an IntentFilter w/ the callback URL + * defined in the OAuthBaseClient. + */ diff --git a/src/com/codepath/oauth/OAuthLoginActivity.java b/src/com/codepath/oauth/OAuthLoginActivity.java index 855305e..53d4a8a 100644 --- a/src/com/codepath/oauth/OAuthLoginActivity.java +++ b/src/com/codepath/oauth/OAuthLoginActivity.java @@ -6,7 +6,7 @@ import com.codepath.utils.GenericsUtil; - +//This is the FragmentActivity supportv4 version of LoginActivity public abstract class OAuthLoginActivity extends FragmentActivity implements OAuthBaseClient.OAuthAccessHandler {