问题
Hi i am integrating twitter to android through twitter4j.I can successfully read tht tweets posted by me.
Now i amtrying to posting tweet from it.but i cant.
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 me can tpost by read only app.
pls help me to sort it out
回答1:
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!
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/4861729/twitter-update-status