As an open-source project, codepath/android-oauth-handler has picked up 116 stars on GitHub (Java). Android OAuth Wrapper makes authenticating with APIs simple
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
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.
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.
Getting Started
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.
Creating a REST Client
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:
public class TwitterClient extends OAuthBaseClient {
public static final BaseApi REST_API_INSTANCE = TwitterApi.instance();
public static final String REST_URL = "https://api.twitter.com/1.1";
public static final String REST_CONSUMER_KEY = "SOME_KEY_HERE";
public static final String REST_CONSUMER_SECRET = "SOME_SECRET_HERE";
public static final String REST_CALLBACK_URL = "oauth://arbitraryname.com";
public TwitterClient(Context context) {
super(context, REST_API_INSTANCE, REST_URL,
REST_CONSUMER_KEY, REST_CONSUMER_SECRET, null, REST_CALLBACK_URL);
}
// ENDPOINTS BELOW
public void getHomeTimeline(int page, JsonHttpResponseHandler handler) {
String apiUrl = getApiUrl("statuses/home_timeline.json");
RequestParams params = new RequestParams();
params.put("page", String.valueOf(page));
client.get(apiUrl, params, handler);
}
}
Configure the REST_API_INSTANCE, 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_INSTANCE should be the instance defining the service you wish to connect to. Check out the full list of services you can select (i.e FlickrApi.instance()).
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:
In TwitterClient.java, the value for REST_CALLBACK_URL must also use the x-oauthflow-twitter protocol.
public static final String REST_CALLBACK_URL = "x-oauthflow-twitter://arbitraryname.com";
Note that the arbitraryname.com value can be any string. If you leave the setting unchecked for callback locking in your Twitter developer settings, then you can use any placeholder value. The callback host value in AndroidManifest.xml must correspond to the part after the :// in REST_CALLBACK_URL, but it need not match the setting in your Twitter developer settings page.
Creating a LoginActivity
The next step to add support for authenticating with a service is to create a LoginActivity which is responsible for the task:
public class LoginActivity extends OAuthLoginActivity<FlickrClient> {
// This fires once the user is authenticated, or fires immediately
// if the user is already authenticated.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
@Override
public void onLoginSuccess() {
Intent i = new Intent(this, PhotosActivity.class);
startActivity(i);
}
// Fires if the authentication process fails for any reason.
@Override
public void onLoginFailure(Exception e) {
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.
public void loginToRest(View view) {
getClient().connect();
}
}
A few notes for your LoginActivity:
Your activity must extend from OAuthLoginActivity<SomeRestClient>
Your activity must implement onLoginSuccess and onLoginFailure
The onLoginSuccess should 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.
In more advanced cases where you want to authenticate multiple services from a single activity, check out the related
guide for using OAuthLoginFragment.
Using the REST Client
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.java
RestClient client = RestClientApp.getRestClient();
client.getHomeTimeline(1, new JsonHttpResponseHandler() {
public void onSuccess(int statusCode, Headers headers, JSON json) {
// Response is automatically parsed into a JSONArray
// json.jsonArray.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(int statusCode, Header[] headers, JSONObject json). If the endpoint does not return JSON, then you can use the AsyncHttpResponseHandler:
Check out Android Async HTTP Docs for more request creation details.
Extra Functionality
Access Authorization
Once the request token has been received, an access token is granted by redirecting to the device's browser to allow the user to grant permission on the API provider's web address. The browser is opened using an implicit intent with no intent flags specified:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorizeUrl));
However, specifying intent flags to alter that behavior can be added to the message using the following:
RestClient client = RestApplication.getRestClient();
// Specify the intent flags as desired
client.setRequestIntentFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Trigger authorization
client.connect();
This can be helpful in cases where you must add a flag such as when encountering the android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? error.
Logging Out
You can log out by clearing the access token at any time through the client object:
In order to troubleshoot API calls, you can take advantage of the Stetho library:
Next, initialize Stetho inside your Application object:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
Edit the manifest.xml file in your project. To let the Android operating system know that you have a custom Application class, add an attribute called android:name to the manifest’s application tag and set the value to the name of your custom Application class.
Does codepath/android-oauth-handler have a project website?
No homepage URL was recorded for codepath/android-oauth-handler in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Does codepath/android-oauth-handler have any tags?
TopGit's last sync did not record any GitHub topics for codepath/android-oauth-handler. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on codepath/android-oauth-handler?
The most recent commit recorded on codepath/android-oauth-handler was 5.4 years ago, based on the GitHub push timestamp. The repository has 34 forks — one of the better signals of community interest.
Is codepath/android-oauth-handler open source?
Yes — codepath/android-oauth-handler ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/codepath/android-oauth-handler.
What license does codepath/android-oauth-handler use?
codepath/android-oauth-handler is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about codepath/android-oauth-handler?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/codepath/android-oauth-handler is the definitive source.
Read full README in the tab above.
Still deciding about android-oauth-handler?
One click hands the question to an AI along with this page — see what it says about android-oauth-handler.