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.
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
Twitterclass which is authorised.You do following in order to get the timeline:
After successful OAuth verification, you get the
oauth_verifiervariable, and then create anAccessTokenwith the following lines:Save the above
tokenandsecretsomewhere, it will be used in generating theAccessTokenlater.Now, the code to update status:
This will update the message
My First Status Updateto your timeline and will get theIDof this status in thestatusIdvariable.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!