This library is an Android library for managing OAuth requests with an extremely easy approach that keeps the details of the OAuth process abstracted from the end-user developer.
This library leverages a few key libraries underneath to power the functionality:
- scribe-java - Simple OAuth library for handling the authentication flow.
- Android Async HTTP - Simple asynchronous HTTP requests with JSON parsing.
You first need to make sure to download the prerequisites for using this library:
Next download the codepath-oauth.jar 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 android-rest-client-template instead and using that as the template for your project.
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.
The first step is to create a REST Client that will be used to access the authenticated APIs within your application. A REST Client is defined in the structure below:
publicclassTwitterClientextendsOAuthBaseClient {
publicstaticfinalClass<? extendsApi> REST_API_CLASS = TwitterApi.class;
publicstaticfinalStringREST_URL = "http://api.twitter.com";
publicstaticfinalStringREST_CONSUMER_KEY = "SOME_KEY_HERE";
publicstaticfinalStringREST_CONSUMER_SECRET = "SOME_SECRET_HERE";
publicstaticfinalStringREST_CALLBACK_URL = "oauth://arbitraryname.com";
publicTwitterClient(Contextcontext) {
super(context, REST_API_CLASS, REST_URL,
REST_CONSUMER_KEY, REST_CONSUMER_SECRET, REST_CALLBACK_URL);
}
// ENDPOINTS BELOWpublicvoidgetHomeTimeline(intpage, AsyncHttpResponseHandlerhandler) {
StringapiUrl = getApiUrl("statuses/home_timeline.json");
RequestParamsparams = newRequestParams();
params.put("page", String.valueOf(page));
client.get(apiUrl, params, handler);
}
}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 you wish to connect to. Check out the full list of services you can select (i.e FlickrApi.class).
Make sure that the project's AndroidManifest.xml has the appropriate intent-filter tags that correspond
with the REST_CALLBACK_URL defined in the client:
<activity ...>
<intent-filter>
<actionandroid:name="android.intent.action.VIEW" />
<categoryandroid:name="android.intent.category.DEFAULT" />
<categoryandroid:name="android.intent.category.BROWSABLE" />
<dataandroid:scheme="oauth"android:host="arbitraryname.com"
/>
</intent-filter>
</activity>If the manifest does not have a matching intent-filter then the OAuth flow will not work.
The next step to add support for authenticating with a service is to create a LoginActivity which is responsible for the task:
publicclassLoginActivityextendsOAuthLoginActivity<FlickrClient> {
// This fires once the user is authenticated, or fires immediately// if the user is already authenticated.@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
@OverridepublicvoidonLoginSuccess() {
Intenti = newIntent(this, PhotosActivity.class);
startActivity(i);
}
// Fires if the authentication process fails for any reason.@OverridepublicvoidonLoginFailure(Exceptione) {
e.printStackTrace();
}
// Method to be called to begin the authentication process// assuming user is not authenticated.// Typically used as an event listener for a button for the user to press.publicvoidloginToRest(Viewview) {
getClient().connect();
}
}A few notes for your LoginActivity:
- Your activity must extend from
OAuthLoginActivity<SomeRestClient> - Your activity must implement
onLoginSuccessandonLoginFailure - The
onLoginSuccessshould launch an "authenticated" activity. - The activity should have a button or other view a user can press to trigger authentication
- Authentication is initiated by invoking
getClient().connect()within the LoginActivity.
- Authentication is initiated by invoking
In more advanced cases where you want to authenticate multiple services from a single activity, check out the related guide for using OAuthLoginFragment.
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
with a JsonHttpResponseHandler handler:
// SomeActivity.javaRestClientclient = RestClientApp.getRestClient();
client.getHomeTimeline(1, newJsonHttpResponseHandler() {
publicvoidonSuccess(JSONArrayjson) {
// 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:
RestClientclient = RestClientApp.getRestClient();
client.get("http://www.google.com", newAsyncHttpResponseHandler() {
@OverridepublicvoidonSuccess(Stringresponse) {
System.out.println(response);
}
});Check out Android Async HTTP Docs for more request creation details.