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


#1

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.

package twitter4j.examples.tweets;

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

import java.io.IOException;

public final class UpdateStatus {

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

	String testPost = "hello from otc";
            //snip
	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.setOAuthAccessToken(accestoken);
		Status status = twitter.updateStatus(testPost);
		System.out.println("it worked!");
		if (status.getId() == 0) {
			System.out
					.println("Error occured while posting tweets to twitter");
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

}

what am i doing wrong here?


#2

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

Tom

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


#3

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.


#4

right the cb…

forgot about those keys… thanks!


#5

package twitter4j.examples.tweets;

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

import java.io.IOException;

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);
twitter.setOAuthAccessToken(accestoken);
Status status = twitter.updateStatus(testPost);
System.out.println(“it worked!”);
if (status.getId() == 0) {
System.out
.println(“Error occured while posting tweets to twitter”);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

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?


#6

You might find some more help for these kinds of library-specific issues on the Twitter4J mailing list: http://groups.google.com/group/twitter4j


#7

[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”


#8

If you’ve gotten to this point, that’s great :slight_smile:

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 http://twitter.com/settings/applications 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).


#9

i see. will try. thanks!

hopefully it works.


#10

holy crap thats it!!!

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

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


#11

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.


#12

are there any sort of styling allowed?

maybe creating a table or linebreaks?

or are the tweets just 100% text only?


#13

Can I have complede code plz…!!


#14

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©;

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 = ter.post(t);
}


#15

this is the whic I have seen from Twitter API.

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


#16
ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessToken) .setOAuthAccessTokenSecret(accessSecret);
		try {
			TwitterFactory factory = new TwitterFactory(cb.build());
			Twitter twitter = factory.getInstance();

			twitter.updateStatus(tweet);

			System.out.println("it worked!");

		} catch (Exception e) {
			e.printStackTrace();
		}

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


#17

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 …


#18

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/auth.py” 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?


#19

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


#20

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