Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Forked from Pretz/gist:822036
Created May 5, 2011 03:37
Show Gist options
  • Save mcxiaoke/956493 to your computer and use it in GitHub Desktop.
Save mcxiaoke/956493 to your computer and use it in GitHub Desktop.
xAuth in Android for Instapaper using Signpost
protected String[] doInBackground(String... credentials) {
String consumer_secret = mActivity.getString(R.string.oauth_consumer_secret);
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("https://www.instapaper.com/api/1/oauth/access_token");
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(mActivity.getString(R.string.oauth_consumer_key),
consumer_secret);
List<BasicNameValuePair> params = Arrays.asList(
new BasicNameValuePair("x_auth_username", credentials[0]),
new BasicNameValuePair("x_auth_password", credentials[1]),
new BasicNameValuePair("x_auth_mode", "client_auth"));
UrlEncodedFormEntity entity = null;
try {
entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("wtf");
}
request.setEntity(entity);
try {
HttpRequest signedRequest = consumer.sign(request);
} catch (OAuthMessageSignerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpResponse response;
InputStream data = null;
try {
response = client.execute(request);
data = response.getEntity().getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String responseString = null;
try
{
final char[] buffer = new char[0x10000];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(data, HTTP.UTF_8);
int read;
do
{
read = in.read(buffer, 0, buffer.length);
if (read > 0)
{
out.append(buffer, 0, read);
}
} while (read >= 0);
in.close();
responseString = out.toString();
} catch (IOException ioe)
{
throw new IllegalStateException("Error while reading response body", ioe);
}
return TextUtils.split(responseString, "&");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment