Twitter + OAuth Integration

Quickly share your Android Code Snippets here...

Twitter + OAuth Integration

Postby vdkhakhkhar » Thu Sep 16, 2010 8:22 am

Anybody can please help in Android + Twitter Integration using OAuth.

I already worked on http://github.com/brione/Brion-Learns-OAuth and getting the error listed below, when I am posting status update...

WARN/System.err(190): org.apache.http.client.HttpResponseException: Unauthorized WARN/System.err(190): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71) WARN/System.err(190): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59) WARN/System.err(190): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657) WARN/System.err(190): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) WARN/System.err(190): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) WARN/System.err(190): at com.test.twitter.BLOA$PostTask.doInBackground(BLOA.java:343) WARN/System.err(190): at com.test.twitter.BLOA$PostTask.doInBackground(BLOA.java:1) WARN/System.err(190): at android.os.AsyncTask$2.call(AsyncTask.java:185) WARN/System.err(190): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256) WARN/System.err(190): at java.util.concurrent.FutureTask.run(FutureTask.java:122) WARN/System.err(190): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648) WARN/System.err(190): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673) WARN/System.err(190): at java.lang.Thread.run(Thread.java:1060)

I succeed with OAuth Authentication and getting user_secret and user_token and stored in preferences...

So the issue is with http posting using OAuth header...

and My Http Post Method is as :
Code: Select all
private class PostTask extends AsyncTask<String, Void, JSONObject> {

  ProgressDialog postDialog;

  @Override
  protected void onPreExecute() {
   postDialog = ProgressDialog.show(BLOA.this,
     getText(R.string.tweet_progress_title),
     getText(R.string.tweet_progress_text), true, // indeterminate
                 // duration
     false); // not cancel-able
  }

  @Override
  protected JSONObject doInBackground(String... params) {

   JSONObject jso = null;
   try {
    HttpPost post = new HttpPost(
      "http://twitter.com/statuses/update.json");
    LinkedList<BasicNameValuePair> out = new LinkedList<BasicNameValuePair>();
    out.add(new BasicNameValuePair("status", params[0]));
    post.setEntity(new UrlEncodedFormEntity(out, HTTP.UTF_8));
    post.setParams(getParams());
    // sign the request to authenticate
    mConsumer.sign(post);
    String response = mClient.execute(post,
      new BasicResponseHandler());
    jso = new JSONObject(response);
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   } catch (OAuthMessageSignerException e) {
    e.printStackTrace();
   } catch (OAuthExpectationFailedException e) {
    e.printStackTrace();
   } catch (OAuthCommunicationException e) {
    e.printStackTrace();
   } catch (ClientProtocolException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } catch (JSONException e) {
    e.printStackTrace();
   } finally {

   }
   return jso;
  }

  // This is in the UI thread, so we can mess with the UI
  protected void onPostExecute(JSONObject jso) {
   postDialog.dismiss();
   if (jso != null) { // authorization succeeded, the json object
     // contains the user information
    mEditor.setText("");
    mLast.setText(getCurrentTweet(jso));
   } else {
    mLast.setText(getText(R.string.tweet_error));
   }
  }
}
TwitterExample.zip
Please add your consumer key & consumer secret to Keys.java
(405.49 KiB) Downloaded 336 times
Vishal Khakhkhar
(Android Programmer)
vdkhakhkhar
Freshman
Freshman
 
Posts: 5
Joined: Thu Nov 12, 2009 6:13 am

Top

Re: Twitter + OAuth Integration

Postby shubh » Thu Sep 23, 2010 8:53 am

Hi Vishal , Have u get any solution , My problem like we\hen I Integrate it my App n when i try to post it generate
09-23 13:19:40.039: WARN/System.err(420): org.apache.http.client.HttpResponseException: Forbidden


It direclly jump from

String response = mClient.execute(post, new BasicResponseHandler());

to catch
(
ClientProtocolException e)
{ e.printStackTrace(); }



when i perform Back button On Authentication screen or at main UI screen it navigate me some Browser Screen .


Please suggest me asap how can i handle it.
Thank in Advance
User avatar
shubh
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Tue May 18, 2010 7:17 am
Location: India

Re: Twitter + OAuth Integration

Postby yorick_1525 » Wed Jun 22, 2011 9:44 pm

if you get "org.apache.http.client.HttpResponseException: Forbidden" but in browser it works, then try to set header: "post.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");"
yorick_1525
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jun 22, 2011 9:43 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests