help with twitter4j: getting 401:Authentication credentials even though ive set them already?

@yao_jiang yao jiang

so im trying to set up this code such that when i run it itll post to my twitter whatever i set "testPost" to be.
i will be getting data from a db once this thing is working.

  1. package twitter4j.examples.tweets;
  3. import twitter4j.Status;
  4. import twitter4j.Twitter;
  5. import twitter4j.TwitterFactory;
  6. import twitter4j.auth.AccessToken;
  7. import twitter4j.conf.*;
  9. import;
  11. public final class UpdateStatus {
  13.     public static void main(String[] args) throws IOException {
  15.        String testPost = "hello from otc";
  16.                 //snip
  17.        ConfigurationBuilder cb = new ConfigurationBuilder();
  19.      cb.setDebugEnabled(true)
  20.            .setOAuthConsumerKey(consumerKey)
  21.           .setOAuthConsumerSecret(consumerSecret)
  22.             .setOAuthAccessToken(accessToken)
  23.           .setOAuthAccessTokenSecret(accessSecret);
  25.      try {
  26.           TwitterFactory factory = new TwitterFactory();
  27.          Twitter twitter = factory.getInstance();
  28.            AccessToken accestoken = new AccessToken(accessToken, accessSecret);
  30.           twitter.setOAuthAccessToken(accestoken);
  31.            Status status = twitter.updateStatus(testPost);
  32.             System.out.println("it worked!");
  33.             if (status.getId() == 0) {
  34.              System.out
  35.                      .println("Error occured while posting tweets to twitter");
  36.            }
  37.       } catch (Exception e) {
  38.             e.printStackTrace();
  39.        }
  40.   }
  41. }

what am i doing wrong here?

2 years 40 weeks ago


@TvdW Tom van der Woerdt

I've never used Twitter4J, but I see that you are creating the "cb" variable but not using it. This is quite possibly related.


PS: You posted your keys in the code - never do that! You wouldn't want to paste passwords either, would you? ;-)

2 years 40 weeks ago
@yao_jiang yao jiang

package twitter4j.examples.tweets;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.*;


public final class UpdateStatus {

public static void main(String[] args) throws IOException {

String testPost = "hello from otc";
String consumerKey = "MY_CONSUMER_KEY";
String consumerSecret = "MY_CONSUMER_SECRET";
String accessToken = "MY_ACCESS_TOKEN";
String accessSecret = "MY_ACCESS_SECRET";

// ConfigurationBuilder cb = new ConfigurationBuilder();
// cb.setDebugEnabled(true)
// .setOAuthConsumerKey(consumerKey)
// .setOAuthConsumerSecret(consumerSecret)
// .setOAuthAccessToken(accessToken)
// .setOAuthAccessTokenSecret(accessSecret);

try {
TwitterFactory factory = new TwitterFactory();
Twitter twitter = factory.getInstance();
AccessToken accestoken = new AccessToken(accessToken, accessSecret);

// twitter.setOAuthConsumer(consumerKey, consumerSecret);
Status status = twitter.updateStatus(testPost);
System.out.println("it worked!");
if (status.getId() == 0) {
.println("Error occured while posting tweets to twitter");
} catch (Exception e) {

ok so ive changed it a bit, i see that cb is not even used.
when i use twitter.setOAuthConsumer(); it says the keys are already set. so i commented that out.
why is it still broken?

2 years 40 weeks ago
@episod Taylor Singletary

You might find some more help for these kinds of library-specific issues on the Twitter4J mailing list:

2 years 40 weeks ago
@yao_jiang yao jiang

[Wed Jul 13 13:53:47 EDT 2011]{"error":"Read-only application cannot POST","request":"\/1\/statuses\/update.json"}

do you think this has something to do with my application?

my oauth setting says "Read and write"

my access token access level says "Read only"

2 years 40 weeks ago
@episod Taylor Singletary

If you've gotten to this point, that's great :)

You must have negotiated your access token while your app was only configured for read-only access. To re-negotiate your token, first revoke it at then wait 5 minutes to be sure our caches clear. Then obtain your access token again -- it should then be renegotiated at the right access level (and the strings for access token and access token secret will change).

2 years 40 weeks ago
@yao_jiang yao jiang

i see. will try. thanks!

hopefully it works.

2 years 40 weeks ago
@yao_jiang yao jiang

holy crap thats it!!!

thank you!!! thank you so much for been patient with me and answering my every question :P

now one last question cause i dont want to spam the forum:
does tweet accept html tags?

what i will be tweeting is more than likely stock prices
and i want to put a symbol with a price and attach a link to that symbol, meaning if a follower clicks on the symbol, lets say YHOO, a new window opens to the URL i specified

2 years 40 weeks ago
@episod Taylor Singletary

Tweets don't support HTML -- it's common to just use stock tickers with a microformat of $YHOO for example. You can otherwise include a link at the end of the tweet.

2 years 40 weeks ago
@yao_jiang yao jiang

are there any sort of styling allowed?

maybe creating a table or linebreaks?

or are the tweets just 100% text only?

2 years 40 weeks ago
@episod Taylor Singletary

Hi Yao,

You'll want to regenerate your API keys/secrets right away -- this is a public forum and it's best not to post consumer keys, secrets, access token secrets, etc -- it provides all the info needed to masquerade as your application and account. Let me know if you need help regenerating those.

2 years 40 weeks ago
@yao_jiang yao jiang

right the cb...

forgot about those keys... thanks!

2 years 40 weeks ago
@rakesh_b143 rakesh

Can I have complede code plz...!!

2 years 40 weeks ago
@rakesh_b143 rakesh

Hi, I am using OAuth Authentication for android. Which method I have to use to authenticate twitter. I used the fallowing method. But not able to authenticate plz help me.

Token = new Token("token_access", "token_secret");
Credential c = new Credential("user_name", "consumer_key", "consumer_secret", token);

UserAccountManager m = UserAccountManager.getInstance(c);

if (m.verifyCredential()) {
GeoLocation loc = new GeoLocation("+37.5", "+26.7");
Tweet t = new Tweet("Cool! Geo-located tweet via Twitter API ME. \o/", loc);
TweetER ter = TweetER.getInstance(m);
t =;

2 years 40 weeks ago
@rakesh_b143 rakesh

this is the whic I have seen from Twitter API.

Can u plz help me which API/ methods I have to use in ANDROID.

2 years 40 weeks ago
@yao_jiang yao jiang
  1. ConfigurationBuilder cb = new ConfigurationBuilder();
  2.            cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
  3.                   .setOAuthConsumerSecret(consumerSecret)
  4.                     .setOAuthAccessToken(accessToken)
  5.                   .setOAuthAccessTokenSecret(accessSecret);
  7.          try {
  8.               TwitterFactory factory = new TwitterFactory(;
  9.                Twitter twitter = factory.getInstance();
  11.               twitter.updateStatus(tweet);
  13.               System.out.println("it worked!");
  15.            } catch (Exception e) {
  16.                 e.printStackTrace();
  17.            }

this works fine for me.. im using config builder so dont need a properties file.

2 years 40 weeks ago
@ShashankKukreti Shashank kukreti

hi i have been creating a twitter application however i am getting following error :

java.lang.NoClassDefFoundError: Could not initialize class twitter4j.TwitterFactory

though i have included the twiiter4j.jar in the classpath, can u please help me .....

2 years 19 weeks ago
@adhavanj Adhavan Jeyachandran

did you get any solution??? i am also facing the same problem

1 year 48 weeks ago
@gvfnix Фролов Григорий

Hello, could anybody explain, what happend to OAuth in twitter? I am developing an Internet site and everything worked fine until this morning. Now I get 401 reply:
File "/usr/local/lib/python2.7/dist-packages/tweepy/" in get_authorization_url
103. raise TweepError(e)

Exception Type: TweepError at /login/twitter
Exception Value: HTTP Error 401: Unauthorized

As you can see I use tweepy plugin.
I tried to reset consumer key and secret, regenereated my access_token even, but that had no affect. What can I do to get this back to work?

2 years 19 weeks ago
@kurrik Arne Roomann-Kurrik

Is there any way you could provide a dump of a failing request? Or more detailed request/response information?

2 years 19 weeks ago
@Mobile_Konstant konstantInfoSolution

i have a same problem but when i change the app permission under setting -> Application Type -> Access -> Read and Write it works and my app is running fine. @45

1 year 45 weeks ago
@karan_g89 karan gulabani

401 error is mostly due to timezones not being in sync. Have a look here:

1 year 21 weeks ago
@MartinMatrix Martin Renner

everything under the sun is in tune, but the sun is eclipsed by the moon .. ..

43 weeks 2 days ago