-
Notifications
You must be signed in to change notification settings - Fork 7
OAuth 1.0 Implementation
Requirements:
-An API class with the correct OAuth Urls
-The API key and secret received when you registered the application
Getting an Access Token
The OAuth 1.0 process is slightly more complicated than the newer version but still fairly simple.
The flow is essentially:
-Get a request token
-Use that request token and a webview to allow the user to enter their credentials and authorize your application
-Upgrade the token to an access token
-
Get a request token
-make a new OAuth10Service by passing in an instance of the API class, your API key, API secret, and an interface callback.
-set any additional parameters, likely the API callback
-call start on the service to get a request tokenOAuth10Service service = OAuthService.newInstance(new TwitterApi(), APIKEY, APISECRET, new OAuth10ServiceCallback() {
@Override public void onOAuthRequestTokenReceived() { getUserAuthorization(); } @Override public void onOAuthAccessTokenReceived(OAuth10Token token) { updateStatus(token); } @Override public void onOAuthRequestTokenFailed(HootResult result) { } @Override public void onOAuthAccessTokenFailed(HootResult result) { } }); service.setApiCallback(CALLBACK); service.start();
2) Use that request token and a webview to allow the user to enter their credentials and authorize your application
- after receiving the request token callback call a method to launch a webview with the authorize url
- catch the url redirect in the webview and upgrade the token
final WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Checking for our successful callback
if(url.startsWith(CALLBACK)) {
webview.setVisibility(View.GONE);
service.getOAuthAccessToken(url);
}
return super.shouldOverrideUrlLoading(view, url);
}
});
webview.loadUrl(service.getAuthorizeUrl());
3) Upgrade the token to an access token
-call getAccessToken on the url received during the webview redirect
-get the OAuth10Token, an object that holds an 'access token' and 'user secret', in the interface callback method
**Making Signed Requests**
Once you have an OAuth10Token, you can begin making signed requests on the API. All you need is the token, an instance of the OAuth10Service, and the url.
For example, to update a user's twitter status:
String baseUrl = "https://api.twitter.com/1/statuses/update.json";
OAuth10Request request = OAuthRequest.newInstance(baseUrl, token, service, new OnRequestCompleteListener() {
@Override
public void onSuccess(HootResult result) {
//process json response string
}
@Override
public void onNewAccessTokenReceived(OAuth20Token token) {
}
@Override
public void onFailure(HootResult result) {
}
});
Map<String,String> queryParameters = new HashMap<String,String>();
queryParameters.put("status", "that's all folks");
request.setRequestParams(queryParameters);
request.post();
You can set any POST or GET parameters by passing in a Map with the key and value and calling either .post() or .get().
You can also set additional header parameters by calling setHeaders(Map<String,String> headers) in the same way.