|
| 1 | +# CodePath OAuth Handler |
| 2 | + |
| 3 | +This library is an Android library for managing OAuth requests with an extremely easy |
| 4 | +approach that keeps the details of the OAuth process abstracted from the end-user developer. |
| 5 | + |
| 6 | +This library leverages a few key libraries underneath to power the functionality: |
| 7 | + |
| 8 | + * [scribe-java](https://github.com/fernandezpablo85/scribe-java) - Simple OAuth library for handling the authentication flow. |
| 9 | + * [Android Async HTTP](https://github.com/loopj/android-async-http) - Simple asynchronous HTTP requests with JSON parsing. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Just download the latest [jar file](https://www.dropbox.com/s/r5m4k3is7nmg15q/codepath-oauth-0.1.0.jar) for this library and drop the jar into your "libs" folder in your Android project. |
| 14 | + |
| 15 | +## Getting Started |
| 16 | + |
| 17 | +This library is very simple to use and simply requires you to create an Activity that is used for authenticating with OAuth and ultimately give your application access to an authenticated API. |
| 18 | + |
| 19 | +### Creating a REST Client |
| 20 | + |
| 21 | +The first step is to create a REST Client that will be used to access the authenticated APIs |
| 22 | +within your application. A REST Client is defined in the structure below: |
| 23 | + |
| 24 | +```java |
| 25 | +public class TwitterClient extends OAuthBaseClient { |
| 26 | + public static final Class<? extends Api> REST_API_CLASS = TwitterApi.class; |
| 27 | + public static final String REST_URL = "http://api.twitter.com"; |
| 28 | + public static final String REST_CONSUMER_KEY = "SOME_KEY_HERE"; |
| 29 | + public static final String REST_CONSUMER_SECRET = "SOME_SECRET_HERE"; |
| 30 | + public static final String REST_CALLBACK_URL = "oauth://arbitraryname"; |
| 31 | + |
| 32 | + public TwitterClient(Context context) { |
| 33 | + super(context, REST_API_CLASS, REST_URL, |
| 34 | + REST_CONSUMER_KEY, REST_CONSUMER_SECRET, REST_CALLBACK_URL); |
| 35 | + } |
| 36 | + |
| 37 | + // ENDPOINTS BELOW |
| 38 | + |
| 39 | + public void getHomeTimeline(int page, AsyncHttpResponseHandler handler) { |
| 40 | + String apiUrl = getApiUrl("statuses/home_timeline.json"); |
| 41 | + RequestParams params = new RequestParams(); |
| 42 | + params.put("page", String.valueOf(page)); |
| 43 | + client.get(apiUrl, params, handler); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Configure the `REST_API_CLASS`, `REST_URL`, `REST_CONSUMER_KEY`, `REST_CONSUMER_SECRET` based on the values needed to connect to your particular API. The `REST_URL` should be the base URL used for connecting to the API (i.e `https://api.twitter.com`). The `REST_API_CLASS` should be the class defining the [service](https://github.com/fernandezpablo85/scribe-java/tree/master/src/main/java/org/scribe/builder/api) you wish to connect to. Check out the [full list of services](https://github.com/fernandezpablo85/scribe-java/tree/master/src/main/java/org/scribe/builder/api) you can select (i.e `FlickrApi.class`). |
| 49 | + |
| 50 | +### Creating a LoginActivity |
| 51 | + |
| 52 | +The next step is to create a `LoginActivity` to use for logging into an OAuth service: |
| 53 | + |
| 54 | +```java |
| 55 | +public class LoginActivity extends OAuthLoginActivity<FlickrClient> { |
| 56 | + // This fires once the user is authenticated, or fires immediately |
| 57 | + // if the user is already authenticated. |
| 58 | + @Override |
| 59 | + public void onLoginSuccess() { |
| 60 | + Intent i = new Intent(this, PhotosActivity.class); |
| 61 | + startActivity(i); |
| 62 | + } |
| 63 | + |
| 64 | + // Fires if the authentication process fails for any reason. |
| 65 | + @Override |
| 66 | + public void onLoginFailure(Exception e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + |
| 70 | + // Method to be called to begin the authentication process |
| 71 | + // assuming user is not authenticated. |
| 72 | + // Typically used as an event listener for a button for the user to press. |
| 73 | + public void loginToRest(View view) { |
| 74 | + getClient().connect(); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +A few notes for your `LoginActivity`: |
| 80 | + |
| 81 | + * Your activity must extend from `OAuthLoginActivity<SomeRestClient>` |
| 82 | + * Your activity must implement `onLoginSuccess` and `onLoginFailure` |
| 83 | + * The `onLoginSuccess` should launch an "authenticated" activity. |
| 84 | + * The activity should have a button or other view a user can press to trigger authentication. |
| 85 | + |
| 86 | +### Using the REST Client |
| 87 | + |
| 88 | +These endpoint methods will automatically execute asynchronous requests signed with the authenticated access token anywhere your application. To use JSON endpoints, simply invoke the method |
| 89 | +with a `JsonHttpResponseHandler` handler: |
| 90 | + |
| 91 | +```java |
| 92 | +// SomeActivity.java |
| 93 | +RestClient client = RestClientApp.getRestClient(); |
| 94 | +client.getHomeTimeline(1, new JsonHttpResponseHandler() { |
| 95 | + public void onSuccess(JSONArray json) { |
| 96 | + // Response is automatically parsed into a JSONArray |
| 97 | + // json.getJSONObject(0).getLong("id"); |
| 98 | + } |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +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`: |
| 103 | + |
| 104 | +```java |
| 105 | +RestClient client = RestClientApp.getRestClient(); |
| 106 | +client.get("http://www.google.com", new AsyncHttpResponseHandler() { |
| 107 | + @Override |
| 108 | + public void onSuccess(String response) { |
| 109 | + System.out.println(response); |
| 110 | + } |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +Check out [Android Async HTTP Docs](http://loopj.com/android-async-http/) for more request creation details. |
0 commit comments