0

I am integrating Twitter to Android through twitter4j. I can successfully read tweets posted by me.

Now I am trying to posting tweet from it, but I can't. I am getting strange warning as below:

    02-01 16:28:43.298: WARN/System.err(729): 401:Authentication credentials were missing or incorrect.
02-01 16:28:43.308: WARN/System.err(729): {"request":"\/1\/statuses\/update.json","error":"Read-only application cannot POST"}
02-01 16:28:43.308: WARN/System.err(729): TwitterException{exceptionCode=[15bb6564-00e54a7f], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.7-SNAPSHOT(build: fd76317d18a608269f0566f73bbb827420c4c77e)}
02-01 16:28:43.308: WARN/System.err(729):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:309)
02-01 16:28:43.318: WARN/System.err(729):     at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:72)
02-01 16:28:43.318: WARN/System.err(729):     at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:103)
02-01 16:28:43.318: WARN/System.err(729):     at twitter4j.Twitter.updateStatus(Twitter.java:501)
02-01 16:28:43.330: WARN/System.err(729):     at com.T4J_OAuth.activities.Main$1.onClick(Main.java:69)
02-01 16:28:43.330: WARN/System.err(729):     at android.view.View.performClick(View.java:2364)
02-01 16:28:43.330: WARN/System.err(729):     at android.view.View.onTouchEvent(View.java:4179)
02-01 16:28:43.339: WARN/System.err(729):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
02-01 16:28:43.339: WARN/System.err(729):     at android.view.View.dispatchTouchEvent(View.java:3709)
02-01 16:28:43.339: WARN/System.err(729):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 16:28:43.349: WARN/System.err(729):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 16:28:43.349: WARN/System.err(729):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 16:28:43.349: WARN/System.err(729):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-01 16:28:43.359: WARN/System.err(729):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-01 16:28:43.359: WARN/System.err(729):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-01 16:28:43.359: WARN/System.err(729):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
02-01 16:28:43.369: WARN/System.err(729):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-01 16:28:43.369: WARN/System.err(729):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-01 16:28:43.378: WARN/System.err(729):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 16:28:43.378: WARN/System.err(729):     at android.os.Looper.loop(Looper.java:123)
02-01 16:28:43.378: WARN/System.err(729):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-01 16:28:43.378: WARN/System.err(729):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 16:28:43.389: WARN/System.err(729):     at java.lang.reflect.Method.invoke(Method.java:521)
02-01 16:28:43.389: WARN/System.err(729):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-01 16:28:43.398: WARN/System.err(729):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-01 16:28:43.398: WARN/System.err(729):     at dalvik.system.NativeStart.main(Native Method)

Though I have set my application in Twitter as read and write, it says I can't post by read only app.

halfer
  • 19,824
  • 17
  • 99
  • 186
chikka.anddev
  • 9,569
  • 7
  • 38
  • 46

3 Answers3

4

Posting a tweet on twitter is different than getting the timeline.

Taking Twitter4J into account the scenarios may be following:

It requires you to have an instance of Twitter class which is authorised.

You do following in order to get the timeline:

Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL); 
...
...

After successful OAuth verification, you get the oauth_verifier variable, and then create an AccessToken with the following lines:

AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,oauth_verifier);
String token = accessToken.getToken(), 
      secret = accessToken.getTokenSecret();

Save the above token and secret somewhere, it will be used in generating the AccessToken later.

Now, the code to update status:

AccessToken accessToken = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken);
Status status = twitter.updateStatus("My First Status Update");
statusId = (int)status.getId();

This will update the message My First Status Update to your timeline and will get the ID of this status in the statusId variable.

For a working example of how to use OAuth to implement sign-in with twitter and get the first tweet of your timeline, click this link to my blog post.

I am sure that after a slight modification into that and the use of above code in it, you can transform it into a tweet posting code too!

Aman Alam
  • 11,231
  • 7
  • 46
  • 81
  • shikh thanks...but it now creates one new prob.After editing ur code i am able to post tweet but now thr is no login oage comes and direct my main layout with button and edit box come and when i post tweet it also posted .Do u think its ri8 behaviour for app.? – chikka.anddev Feb 01 '11 at 11:50
  • It is the right behaviour. that app stores the credentials and uses them everytime you start the app. until one of these happens: a) user revokes permission to ur app, b)you log out of twitter using ur app – Aman Alam Feb 01 '11 at 11:59
  • ok...thnx.so tht means once i login ti will save my credential till i logout through app – chikka.anddev Feb 01 '11 at 12:18
  • yes, either through app, or u revoke permissions from twitter.com – Aman Alam Feb 01 '11 at 12:21
  • @SheikhAman I want to show popup dialog (to update the twitter status) which contains an edit text,cancel button and update status button where user can enter the text in EditText and after clicking on update status button his status will be updated and should give me a response after updating his status ... Have you implemented like this ????? Or is it possible to implement this ?? in android – KK_07k11A0585 Apr 25 '12 at 09:51
  • Where do you get the `oauth_verifier` variable from? Can you update the answer accordingly. – ban-geoengineering Nov 20 '14 at 13:29
1
try {
    AccessToken accessToken = new AccessToken(token,secret);
    Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken);
    shareText= edttweetnow.getText().toString();
    status = twitter.updateStatus(shareText);
    int statusId = (int)status.getId();

    Toast.makeText(TweetLoad.this, "Tweet Successfull", Toast.LENGTH_LONG).show();

} catch (TwitterException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(TweetLoad.this, e.toString(), Toast.LENGTH_LONG).show();
}

Share text is string which stores data of edit text. TweetLoad is current class name. Do it

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
help
  • 11
  • 1
  • 1
0

First line says it: 02-01 16:28:43.298: WARN/System.err(729): 401:Authentication credentials were missing or incorrect.

Either you did not login at all or with the wrong credentials. Logging into Twitter is not trivial anymore with oAuth. But when you already have registered your app, you should have the consumer token + secret to work with.

Have a look at the twitter 4j docs for example code or the Zwitscher source code for an Android app that uses twitter4j do communicate with Twitter.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • thanx heiko but i have used same to get tweets and it works so i think it is not prob. of credentials or of consumer token – chikka.anddev Feb 01 '11 at 11:21
  • There are some requests like public timeline that don't need authentication - see the matrix on twitter4j.org - http://twitter4j.org/en/api-support.html – Heiko Rupp Feb 01 '11 at 11:23