How-To: Using Twitter API ME with Java ME SDK 3.0

  18 posts   Feedicon  
Replies: 17 - Last Post: September 07, 2010 12:13
by: Ernandes Mourao Junior
showing 1 - 18 of 18
 
Posted: August 05, 2010 12:14 by Terrence Barr
All,

I will be using Twitter API ME for a demo project over the
next couple of week so I've spent some time setting up a
little test app. I thought I'd share the info on getting everything
to work.

Basically, it was pretty straightforward and I didn't encounter
any major issues.

Steps:

1. xAuth enable

Request xAuth for your Twitter consumer (application)

2. Equifax cert

Import the Equifax cert that Twitter is using for SSL into
the Java ME SDK 3.0 emulator (NetBeans uses the same
Java ME SDK install so once it works in the SDK it should
also work in NetBeans)

To import the Equifax cert:

#importing to J2SE keystore
keytool -import -v  -trustcacerts -alias equifax -file equifax.crt
-keystore j2se_test_keystore.bin -storepass keystorepwd

#importing to J2ME keystore
java -jar MEKeyTool.jar -import -MEkeystore _main.ks -keystore
j2se_test_keystore.bin -storepass keystorepwd -alias equifax -domain
identified_third_party

Then, copy the _main.ks to the root of the JavaME SDK emulator
instance you are using, for example for the DefaultCldcMsaPhone1

C:\Documents and Settings\<user>\javame-sdk\3.0\work\4\appdb

(for more info on the Java ME SDK and SSL see
http://forums.sun.com/thread.jspa?messageID=10791401)

3. Create MIDP project

Create a MIDP project (for example, in NetBeans) for your
test app. Make sure you include all necessary libraries that come
with Twitter API ME:

xauth-encoders.jar
kxml2-min-2.3.0.jar, 
mobile-ju-framework-1.0.jar
twitter_api_me-1.3.jar 

(all of these must be the ME versions, of course).

Create your test MIDlet. The relevant Twitter API ME code
to log in and post a tweet looks like this:

    Credential credential = new Credential(<twitter-login>,
        <twitter-passwd>, <app-consumerKey>, <app-consumerSecret>);
    UserAccountManager userAccMgr = UserAccountManager.getInstance(credential);
    if (!userAccMgr.verifyCredential()) {
       // fail
    }
    Tweet tweet = new Tweet("Tweet via Twitter API ME.");
    TweetER tweeter = TweetER.getInstance(userAccMgr);
    tweet = tweeter.post(tweet);

Works like a charm.

Hope this helps.

*** Thanks to Ernandes for a great library! ***

PS: The Equifax cert has now been added to the Java ME
SDK default keystore, so in the next update of the SDK the
steps for importing the cert will no longer be necessary.

Best,
-- Terrence
http://terrencebarr.wordpress.com/
 
Posted: August 05, 2010 13:10 by Ernandes Mourao Junior
Hi Terrence,

Thanks for this great tutorial!

For sure, it will be a great asset for this project.

Regards,
Ernandes
 
Posted: August 21, 2010 09:39 by pradeep034045123
Hi Terrence,

I used Twitter api with Lwuit. It works fine in the simulator. but I doesnt work in nokia 95. But I didnt use Auth Api. Can any one tel me where can i fine XAuth.jar. can u please give me a link to download it.

Thanks and Regards,
Pradeep
 
Posted: August 22, 2010 19:50 by Ernandes Mourao Junior
Hi Pradeep,

There is no such xAuth.jar you mentioned. Twitter API ME 1.3 already supports xAuth. To authenticate with using xAuth, please, check the section "Verify User's Credentials" in our Wiki page: http://kenai.com/projects/twitterapime/pages/Home#VerifyUserCreds

Regards,
Ernandes
 
Posted: August 24, 2010 04:31 by pradeep034045
Hi ernandes,

Thanks for the quick reply. Now I registerd my application at twitter. and I got the consumer key and secret key.
What I did was I passed those keys to the constructor like the following

Credential credential = new Credential(user, pwd,Constants.twitConsumerKey,Constants.twitConsumerSecretKey);

but I get the follwing exception,
subject alternative name did not match site name

Can u tel me how can I use that consumer key and secret key

Thanks in advance.
 
Posted: August 24, 2010 11:13 by Ernandes Mourao Junior
Now you have to request to api@twitter.com priveliges to your app to use your xauth keys. However, they are not providing it for personal test purposes. If it is your case, I ask you to wait for versin 1.4, where I am implementing a solution to avoid this step on xAuth usage.

Regarding the excetion message you mentioned, check this thread: http://kenai.com/projects/twitterapime/forums/forum/topics/3113-oAuth-J2ME-Certificate-Error

Regards,
Ernandes
 
Posted: September 01, 2010 07:23 by pradeep034045
Hi, Erandes,

Thanks for u r immediate reply, but,

Im using TwitterMe api for my mobile application, I registered my
application in twitter and got the consumer key and the secret key. and got the priveliges also but
The problem Im having is after I post a twit it gives an exception
which tells

"javax.microedition.pki.CertificateException: Subject alternative name
did not match site name"

This is how I do the post.

Credential credential = new Credential(user,
pwd,Constants.twitConsumerKey,Constants.twitConsumerSecretKey);
UserAccountManager userAccMgr =
UserAccountManager.getInstance(credential);
if (userAccMgr.verifyCredential()) {
Tweet tweet = new Tweet(message);
TweetER tweeter = TweetER.getInstance(userAccMgr);
tweet = tweeter.post(tweet);
DialogFrame frame = new DialogFrame();
frame.setTitle("Twitter");
frame.setBody("Successfully Twitted!");
frame.createUI(2);
}

and also
what are these urls for?
Request token URL
Access token URL
Authorize URL

pls some body help me. Thanks in advance.

pradeep.
 
Posted: September 01, 2010 11:17 by Ernandes Mourao Junior
Hi,

Your code looks fine. Please, see this thread: http://kenai.com/projects/twitterapime/forums/forum/topics/3113-oAuth-J2ME-Certificate-Error

Regards,
Ernandes
 
Posted: September 02, 2010 05:33 by pradeep034045
Hi,

Thanks for the reply, pls tel me how can I use Http to avoid that certification problem.


Thanks & regards,
Pradeep.
 
Posted: September 02, 2010 11:43 by Ernandes Mourao Junior
The only method that access Twitter API using HTTPS is the UserAccountManager.verifyCredential(), when your are using xAuth. We must work that way. However, if you use Single Access token, this access is bypassed and you can test on emulator without problems. But remender it is just a workaround for this issue.
 
Posted: September 03, 2010 01:53 by pradeep034045
HI,

thanks for ur immediate reply, How can I use that single access token to avoid that, pls tel me how to do that.
thanks

pradeep.
 
Posted: September 03, 2010 09:41 by pradeep034045
how can I get equifax certificate and does it support to WTK 2.5.2

Thanks,
Pradeep
 
Posted: September 03, 2010 10:00 by netroboost
Pradeep,

Installing Equifax certificate will not help since WTK 2.5.2 does not accept wild card certificates. That is, it would have recognized http://twitter.com but since we are communicating with http://api.twitter.com it will reject that domain.

To bypass this, here is one way of approaching it:

1. Use the source code of TWAPIME instead of the packaged library.

2. Go to com->twitterapime->rest ->UserAccountManager.java

3. Search for "https://api.twitter.com/oauth/access_token"

4. Change the https to http

It should work fine.

Amit.
 
Posted: September 03, 2010 11:27 by Ernandes Mourao Junior
Amit,

You do not need to change the source code to do that. There is a method in UserAccountManager class where you can replace the urls used by API. See below:

UserAccountManager.setServiceURL(
UserAccountManager.TWITTER_API_URL_SERVICE_OAUTH_ACCESS_TOKEN,
"http://api.twitter.com/oauth/access_token"
);

To let it clear, Twitter API recommends the use of HTPS for this endpoint.

Pradeep,

To auth with Single Access token, see our wiki:

http://kenai.com/projects/twitterapime/pages/Home#VerifyUserCreds

Regards,
Ernandes
 
Posted: September 06, 2010 06:43 by pradeep034045
HI, Eranandes,

Thanks to ur reply. It works fine in the emulator. But when I install that in to my phone, it doesnt work.
pls tel me what do i do.

Thanks,
Pradeep.
 
Posted: September 07, 2010 03:18 by pradeep034045
HI
Amith, this is working fine in emulator , but after installing that application in my mobile phone, it doesnt work.
Pls help me to correct that problem.

Thanks,
Pradeep.
 
Posted: September 07, 2010 12:13 by Ernandes Mourao Junior
Pradeep,

Please, send an e-mail to support@twapime.com explaining your situation so I can help you. This thread is so confuse at this point. As soon as we solve it, we post the conversation here in the forum, in order to help other developers.

Regards,
Ernandes
 
Posted: September 06, 2010 04:32 by mopuruvinod
Hi Terrence,

I imported the libraries:

xauth-encoders.jar
kxml2-min-2.3.0.jar,
mobile-ju-framework-1.0.jar
twitter_api_me-1.3.jar


After using Ernandes library i am receiving errors as below
I used single access token here
verification of credentials are successfull. but failed to Tweet received below errors

In WTK 3.0
javax.microedition.io.ConnectionNotFoundException: ConnectionNotFound error in socket::open : error = 10060

In WTK 2.5.2
java.lang.SecurityException: Invalid / expired Token

Source is as follows:

Token token = new Token("token_access", "token_secret");
Credential c = new Credential("mopuruvinodreddy@gmail.com", "8oHIBK7T90Ye7GjIF6SToA", "4Ebm4f7bie2AAYyN75DxwSUYeg8x0SBlL0IyKIXzaw", token);
UserAccountManager m = UserAccountManager.getInstance(c);
try {
if (m.verifyCredential()) {
System.out.println("Consumer or token keys are success");
Tweet t = new Tweet("Hi!!! This is my first tweet via Twitter API ME.");
TweetER ter = TweetER.getInstance(m);
t = ter.post(t);

} else {
System.out.println("Consumer or token keys are invalid!");
}
} catch (Exception ex) {
System.out.println("Exception here1="+ex);
}

If i use normal credentials my application is not able to validate credentials itself

In WTK 3.0

javax.microedition.io.ConnectionNotFoundException: ConnectionNotFound error in socket::open : error = 10060

In WTK 2.5.2

Credentials unsucessfull

Also when i am not able to import Equifax cert
Please help to authentication, post and receive tweets using my application.

Regards,
Vinod





showing 1 - 18 of 18
Replies: 17 - Last Post: September 07, 2010 12:13
by: Ernandes Mourao Junior
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120127.ac94057)
 
 
Close
loading
Please Confirm
Close