I've been trying to learn how to authenticate an android app to a server using oAuth 1.0a (I know it's out of date, but it's what I'm being forced to use.), and I've been following the tutorial on how to connect an application to oAuth1.0a here
However, after writing the activity that they suggest, i've been encountering an error like the following:
oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: No peer certificate
at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:218)
at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:74)
at com.company.companyone.account.PrepareRequestTokenActivity$OAuthRequestTokenTask.doInBackground(PrepareRequestTokenActivity.java:141)
at com.company.companyone.account.PrepareRequestTokenActivity$OAuthRequestTokenTask.doInBackground(PrepareRequestTokenActivity.java:122)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
The activity that I've written is as follows:
public class PrepareRequestTokenActivity extends Activity {
private CommonsHttpOAuthConsumer consumer;
private CommonsHttpOAuthProvider provider;
private final static String TAG = "PrepareRequestTokenActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prepare_request_token);
try {
System.setProperty("debug", "true");
consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(
Constants.REQUEST_URL,
Constants.ACCESS_URL,
Constants.AUTHORIZE_URL);
provider.setOAuth10a(true);
} catch (Exception e) {
Log.d(TAG, "Error intializing consumer and provider", e);
e.printStackTrace();
}
Log.i(TAG, "Starting task to retrieve request token.");
new OAuthRequestTokenTask(this,consumer,provider).execute();
}
@Override
protected void onResume(){
super.onResume();
//need to get the preferences to store response tokens later.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final Uri uri = getIntent().getData();
//make sure that the callback worked correctly
if(uri != null && uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)){
Log.i(TAG, "Got Callback : uri = " +uri);
Log.i(TAG, "Attempting to Retrieve Access Token");
new RetrieveAccessTokenTask(this,consumer,provider,prefs).execute(uri);
//done
finish();
}else{
Log.d(TAG, "Fatal Error: oAuth Callback malformed.");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_prepare_request_token, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class OAuthRequestTokenTask extends AsyncTask<String, Void, String>{
private CommonsHttpOAuthConsumer reqConsumer;
private CommonsHttpOAuthProvider reqProvider;
private Context context;
public OAuthRequestTokenTask(PrepareRequestTokenActivity prepareRequestTokenActivity, CommonsHttpOAuthConsumer reqConsumer, CommonsHttpOAuthProvider reqProvider){
this.context = prepareRequestTokenActivity.getApplicationContext();
this.reqConsumer = reqConsumer;
this.reqProvider = reqProvider;
}
@Override
protected String doInBackground(String... strings) {
try {
Log.i(TAG, "Retrieving request token from Magento servers");
final String url = reqProvider.retrieveRequestToken(reqConsumer, Constants.OAUTH_CALLBACK_URL);
Log.i(TAG, "Popping a browser with the authorize URL : " + url);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "Error during OAUth retrieve request token", e);
e.printStackTrace();
}
return null;
}
}
private class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
private Context context;
private CommonsHttpOAuthConsumer consumer;
private CommonsHttpOAuthProvider provider;
private SharedPreferences prefs;
//collect the instance variables for the Access Token request
public RetrieveAccessTokenTask(PrepareRequestTokenActivity prepareRequestTokenActivity, CommonsHttpOAuthConsumer consumer, CommonsHttpOAuthProvider provider, SharedPreferences prefs) {
this.context = prepareRequestTokenActivity.getApplicationContext();
this.consumer = consumer;
this.provider = provider;
this.prefs = prefs;
}
//called by .execute()
//takes Verifier Token and uses it to retrieve the Access Token by validating this instance of the app.
@Override
protected Void doInBackground(Uri... params) {
final Uri oauth_uri = params[0];
final String verifier = oauth_uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try{
//set the provider to do it's ish.
provider.retrieveAccessToken(consumer,verifier);
//save the token in the shared preferences so that we can use it in our request methods.
//MAKE SURE YOU LEAVE THIS AS COMMIT. We want this to persist in memory, and apply doesn't guarantee that the token will remain if the app closes immediately.
final SharedPreferences.Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit();
String token = prefs.getString(OAuth.OAUTH_TOKEN,"");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET,"");
consumer.setTokenWithSecret(token,secret);
Toast.makeText(context, "Token Success! += "+ token, Toast.LENGTH_LONG);
//kick it back to the mainactivity.
context.startActivity(new Intent(context, MainActivity.class));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
I'm not certain if the error that I have is caused by a bad SSL certificate from the VM that I'm running the Magento webserver on, or if the error is somewhere in my code. The SSL Certificate is self-signed if that helps so that it can be tested over a private network.
//oAuth Constants
public static final String REQUEST_URL="https://myhost/oauth/initiate";
public static final String ACCESS_URL="https://myhost/oauth/token";
public static final String AUTHORIZE_URL="https://myhost/oauth/authorize ";
This is the line that throws the error.
final String url = reqProvider.retrieveRequestToken(reqConsumer, Constants.OAUTH_CALLBACK_URL);
I traced the error to the CommonsHttpOAuthProvider.sendRequest() method. It breaks at:
HttpResponse response = httpClient.execute((HttpUriRequest) request.unwrap());
Before the request.unwrap() call is executed.
TL;DR: Did I do something wrong, and does Android just not like self-signed SSL certificates?