This is the official android sdk for OAuth.io !
The OAuth.io android sdk allows you to use OAuth for your android application, and connect any OAuth provider available on OAuth.io.
To use this sdk you will need to make sure you've registered your OAuth.io app and have a public key (https://oauth.io/docs).
First you need to create an android project in eclipse for example To install the sdk android just download oauth.jar and put it in your libs directory Then refresh the libs directory in eclipse, right click on the oauth.jar and add it in your build path
You MUST add the line in your AndroidManifest.xml in the manifest tag:
<uses-permission android:name="android.permission.INTERNET" />
In your Activity, you can instantiate a OAuth class:
final OAuth oauth = new OAuth(context);
oauth.initialize('Public key');
To connect your user to a provider (e.g. facebook):
oauth.popup('facebook', callback);
// or (with options a org.json.JSONObject):
oauth.popup('facebook', options, callback);
The callback is a class that implement the OAuthCallback
method.
The OAuthCallback
interface implement just one method :
void onFinished(OAuthData data);
The OAuthData class contain all the OAuth information :
public String provider; // name of the provider
public String state; // state send
public String token; // token received
public String secret; // secret received (only in oauth1)
public String status; // status of the request (succes, error, ....)
public String expires_in; // if the token expires
public String error; // error encountered
The OAuthData
class contains the method http
to fill an http request with the needed infos to authorize an API call.
You can pass the url of the API call (the url can be absolute or relative to the provider's API base url), and an implementation of OAuthRequest
to set your http request's url and add headers to inject your oauth tokens.
The implementation is left to you because this design choice allow you to make the request with your preferred way, which can be URLConnection
/ HttpURLConnection
, HttpRequest
, or any third party library.
data.http("/me", new OAuthRequest() {
@Override
public void onSetURL(String _url) {
// This method is called once the final url is returned.
}
@Override
public void onSetHeader(String header, String value) {
// This method is called for each header to add to the request.
}
@Override
public void onReady() {
// This method is called once url and headers are set.
}
@Override
public void onError(String message) {
// This method is called if an error occured
}
});
See the example for a full implementation using URLConnection
.
For OAuth 1 API requests, the request is proxified via https://oauth.io to sign your request without exposing your secret key. The OAuth 2 API requests are direct since we pass the API request's authorizing description beside the tokens.
-
Create a new project as described in the Android documentation. By example in eclipse:
File -> New -> Other -> Android Project
-
Install OAuth.io Android sdk into the project
-
Replace the generated example res/layout/activity_main.xml , MainActivity.java with the files included in the example folder. Don't forget to add uses-permission android:name="android.permission.INTERNET" to your AndroidManifest.xml. A valid key is provided, but you can do your own app on OAuth.io.
-
Plug your phone & run it ! (or use a virtual device)