My favorites | Sign in
Project Home Downloads Wiki
Search
for
TwitterAndSignpost  
Real-life example of how to use Signpost with Twitter
twitter, Featured, example
Updated Jan 31, 2010 by m.kaepp...@gmail.com

Using Signpost with Twitter

You can get a consumer key and secret for your Twitter application here.

Simple Example - Setting up authentication and GET'ing mentions

        OAuthConsumer consumer = new DefaultOAuthConsumer(
                // the consumer key of this app (replace this with yours)
                "iIlNngv1KdV6XzNYkoLA",
                // the consumer secret of this app (replace this with yours)
                "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM");

        OAuthProvider provider = new DefaultOAuthProvider(
                "http://twitter.com/oauth/request_token",
                "http://twitter.com/oauth/access_token",
                "http://twitter.com/oauth/authorize");

        /****************************************************
         * The following steps should only be performed ONCE
         ***************************************************/

        // we do not support callbacks, thus pass OOB
        String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);

        // bring the user to authUrl, e.g. open a web browser and note the PIN code
        // ...         

        String pinCode = // ... you have to ask this from the user, or obtain it
        // from the callback if you didn't do an out of band request

        // user must have granted authorization at this point
        provider.retrieveAccessToken(consumer, pinCode);

        // store consumer.getToken() and consumer.getTokenSecret(),
        // for the current user, e.g. in a relational database
        // or a flat file
        // ...

        /****************************************************
         * The following steps are performed everytime you
         * send a request accessing a resource on Twitter
         ***************************************************/

        // if not yet done, load the token and token secret for
        // the current user and set them
        consumer.setTokenWithSecret(ACCESS_TOKEN, TOKEN_SECRET);

        // create a request that requires authentication
        URL url = new URL("http://twitter.com/statuses/mentions.xml");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        // sign the request
        consumer.sign(request);

        // send the request
        request.connect();

        // response status should be 200 OK
        int statusCode = request.getResponseCode();

Here is what you typically do step by step:

  1. When your users want to access Twitter through your application for the first time, you should display some kind of notification which indicates that an authentication process is about to start
  2. Call OAuthProvider.retrieveRequestToken(callback) on the OAuthProvider. If your application can receive callbacks via URLs, you can be informed about successful authorization by providing a callback URL here. If you cannot receive callbacks (e.g. because you're developing a desktop application), then you must pass "oob" here (for "out of band").
  3. On method return, you have to send the user to the URL returned by it. The user now must grant your application access to Twitter - this step is out of the reach of your application, because it happens in the Web browser.
  4. Call OAuthProvider.retrieveAccessToken() with the PIN code generated by Twitter in the previous step. If your interaction style is out-of-band, then you must ask the user to enter this value somewhere, since there is no way to get it programatically. If however you defined a callback URL before, then the PIN code will be passed to your application automatically with that callback as an "oauth_verifier" parameter.
  5. On method return, call OAuthConsumer.getToken() and OAuthConsumer.getTokenSecret(), associate them with the user who triggered the authorization procedure, and store them away safely.
  6. Any OAuthConsumer configured with these values can now sign HTTP requests in order to access protected resources on Twitter on behalf of that user.

You can also download this example code as a Java/Eclipse project and go from there. This example application executes all steps above sequentially in the console. If run successfully, its output should be something like this:

 Fetching request token from Twitter...
 Request token: g1re4iYfknOYMB62JZkddjwhCfvfn4WPdZrzgscMOA
 Token secret: wKStK0dmhPcuFxBki3imJv7yVNXgRndmW4LSmuCg
 Now visit:
 http://twitter.com/oauth/authorize?oauth_token=g1re4iYfknOYMB62JZkddjwhCfvfn4WPdZrzgscMOA ... and grant this app authorization
 Enter the PIN code and hit ENTER when you're done:
 xxxxxxx
 Fetching access token from Twitter...
 Access token: 14418463-Xt70aZ8b2jz19MzY7JnQJ6HglPh0Hc5p939gLH5YI
 Token secret: TkG689FOx7amgdX2ta6epE5MYsmZxVuO9ith7FtJrs
 Sending request to Twitter...
 Response: 200 OK
 

Elaborate Example - POST'ing status updates

Thanks Kerry for sharing this via our Google group!

NOTE: This example assumes that you're using Apache HttpClient for HTTP messaging.

We'll need to tweak some of HttpClient's defaults, like so:

    public HttpParams getParams() {
        // Tweak further as needed for your app
        HttpParams params = new BasicHttpParams();
        // set this to false, or else you'll get an
        // Expectation Failed: error
        HttpProtocolParams.setUseExpectContinue(params, false);
        return params;
    }

    // Your actual method might look like this:
    public void updateProfileBackground(User user, File file) {
        try {
            // Create a new consumer using the commons implementation
            OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey,
                    consumerSecret);
            consumer.setTokenWithSecret(getUserAccessToken(user),
                    getUserTokenSecret(user));
            HttpPost uploadBackgroundPost = new HttpPost(
                    UPDATE_PROFILE_BACKGROUND_IMAGE_URL);

            // The body of a multi-part post isn't needed
            // for the generation of the signature
            consumer.sign(uploadBackgroundPost);

            // only works in strict mode
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.STRICT);

            // Twitter checks against supported file types
            FileBody imageBody = new FileBody(file, "image/png");

            entity.addPart("image", imageBody);
            uploadBackgroundPost.setEntity(entity);
            DefaultHttpClient httpClient = new DefaultHttpClient(getParams());

            // If you're interested in the headers,
            // implement and add a request interceptor that prints them
            httpClient.addRequestInterceptor(new PrintRequestInterceptor());

            System.out.println(httpClient.execute(uploadBackgroundPost,
                    new BasicResponseHandler()));
        } catch (Exception e) {
            // do some proper exception handling here
            e.printStackTrace();
        }
    }

Now oddly enough, if the multi-part form data is not being formatted exactly to Twitter's preferences you won't get an error message, but the background image will be set to a broken link! You might see the image url being constructed as:

http://a3.twimg.com/profile_background_images/test.png

instead of:

http://a3.twimg.com/profile_background_images/5463125/test.png (Notice the missing folder)

Instructions for using Apache HttpClient 2.x

Additionally here's an example if you're using Apache HttpClient2.x, which uses a different API than HttpClient 4.x.

First off you'll need to make your own adapter for the MultipartPostMethod (or if you're adventurous HttpMethod more generally).

Something along the lines of:

    public class HttpClient2OAuthConsumer extends AbstractOAuthConsumer {
        public HttpClient2OAuthConsumer(String consumerKey,
                String consumerSecret, SignatureMethod signatureMethod) {
            super(consumerKey, consumerSecret, signatureMethod);
        }

        protected oauth.signpost.http.HttpRequest wrap(Object request) {
            if (!(request instanceof MultipartPostMethod))
                throw new IllegalArgumentException(
                        "This consumer expects requests of type MultipartPostMethod");
            else
                return new HttpRequestAdapter((MultipartPostMethod) request);
        }
    }

And:

    public class HttpRequestAdapter implements oauth.signpost.http.HttpRequest {

        private MultipartPostMethod method;

        public HttpRequestAdapter(MultipartPostMethod method) {
            this.method = method;
        }

        public String getContentType() {
            Header contentHeader = method.getRequestHeader("Content-Type");
            return (contentHeader == null) ? "" : contentHeader.getValue();
        }

        public String getHeader(String s) {
            return method.getRequestHeader(s).getValue();
        }

        public InputStream getMessagePayload() throws IOException {
            return new FileInputStream(method.getFileData());
        }

        public String getMethod() {
            return method.getName();
        }

        public String getRequestUrl() {
            try {
                return method.getURI().getURI();
            } catch (URIException e) {
                e.printStackTrace();
            }
            return null;
        }

        public void setHeader(String s, String s1) {
            method.setRequestHeader(s, s1);
        }

    }

This method will look very similar.

    public void updateProfileBackgroundHttpCommons(User user, File file) {
        try {
            HttpClient client = new HttpClient();
            // make a new consumer with our own implementation
            OAuthConsumer consumer = new HttpClient2OAuthConsumer(consumerKey,
                    consumerSecret);
            consumer.setTokenWithSecret(getUserAccessToken(user),
                    getUserTokenSecret(user));
            MultipartPostMethod mp = new MultipartPostMethod(
                    UPDATE_PROFILE_BACKGROUND_IMAGE_URL);

            FilePart image = new FilePart("image", file, "image/png", null); // again specify the type
            // charset cannot be set to a type.
            // httpclient2.x mistakenly adds one to a filepart by default
            image.setCharSet(null);
            mp.addPart(image);
            mp.setStrictMode(false);

            consumer.sign(mp);
            client.executeMethod(mp);
            for (Header header : mp.getRequestHeaders()) {
                System.out.println(header.toExternalForm());
            }
            System.out.println(mp.getResponseBodyAsString());

        } catch (Exception e) {
            // do some proper exception handling here
            e.printStackTrace();
        }
    }

And that's it.

Comment by f3401...@gmail.com, Jun 10, 2009

authentication fails possibly due to PIN-based process

Comment by kaspar.ru@gmail.com, Jun 12, 2009

the method in DefaultOAuthProvider should be modified to accept pin

public void retrieveAccessToken(String pin)

throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {
if (consumer.getToken() == null ||pin.trim()=="") {
consumer.getTokenSecret() == null pin==null
throw new OAuthExpectationFailedException(
"Authorized request token or token secret not set. "
+ "Did you retrieve an authorized request token before?");
}
retrieveToken(accessTokenEndpointUrl+"?oauth_verifier="+pin);
}

Comment by anthony....@gmail.com, Jun 12, 2009

When I run the example code I get:

Exception in thread "main" java.lang.UnsupportedClassVersionError?: Bad version number in .class file

Comment by arjun.si...@gmail.com, Jun 14, 2009

Thanks for the post.

how to retrieve the response from twitter ?

Comment by project member m.kaepp...@gmail.com, Jun 16, 2009

The library hasn't been updated to implement OAuth 1.0a, yet, so Twitter and Fire Eagle support is broken.

I'll let you guys know when it's working again.

Comment by eelstret...@gmail.com, Jun 17, 2009

Matthias, what's the symptom of the breakage? I'm trying this example using 1.1 (well, a close version of it) and I'm seeing:

oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Already connected
	at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:132)
	at oauth.signpost.basic.DefaultOAuthProvider.retrieveAccessToken(DefaultOAuthProvider.java:82)
Comment by jav...@beboinc.com, Jun 17, 2009

About the "Exception in thread "main" java.lang.UnsupportedClassVersionError??: Bad version number in .class file" error, it happens when you run the code with java 1.5. You need to use java 1.6

Comment by project member m.kaepp...@gmail.com, Jun 18, 2009

Hey guys,

As to the first question: The 1.1 build has a bug in OAuthProvider where the connection is cached incorrectly, causing the already connected exception. Will fix that asap.

As to the UnsupportedClassVersionError?: The zip file containing the example comes with an old build of Signpost (1.0-SNAPSHOT) which was built using JDK6. I already fixed that (Signpost no longer needs Java 6 to run), but I have to update the example archive to ship with the new build.

I have my hands full with work right now, but I will get to these things asap.

Thanks everyone for reporting!

Comment by eelstret...@gmail.com, Jun 18, 2009

Do let us know when the fix is in :-)

BTW, just wanted to say how nice it is to use a straightforward OAuth that has good examples! Keep up the good work!

One thing that would be nice is signpost java-twitter integration, but that might be too much to ask, and perhaps I should take a cut at it myself...

Comment by project member m.kaepp...@gmail.com, Jun 21, 2009

Just updated the lib and the examples to work with 1.0a providers.

Thanks again for your input and let me know if something still doesn't work as expected.

Comment by ajaysha...@gmail.com, Jun 25, 2009

how to get data from twitter using accesstoken?

Comment by acheun...@gmail.com, Jun 30, 2009

Is there anyway to serialize the OAuthProvider for storage so that the OAuthProvider.retrieveAccessToken(String) step can be executed later in a seperate thread? If not, how can you create a new OAuthProvider object to continue with the retrieveAccessToken step?

Thanks.

Comment by project member m.kaepp...@gmail.com, Jul 1, 2009

Hi,

You could simply re-create the OAuthProvider with an OAuthConsumer that has all the token data set from where you left off, i.e. by doing this:

// Thread 1 provider.retrieveRequestToken(); // consumer now has request token + secret set // ... serialize token and secret

// Thread 2: OAuthConsumer consumer = new DefaultOAuthConsumer(...); consumer.setTokenWithSecret(/deserialized token and secret/); OAuthProvider provider = new DefaultOAuthProvider(consumer, ...); provider.retrieveAccessToken();

I didn't test this, but it should work.

A better solution however would simply be to declare both classes serializable. Both are very simple objects carrying almost no state. The provider holds a URLConnection reference, but that can safely be declared transient.

Can you open a ticket on the issue tracker for this please? I'll look into this in more detail then.

Comment by pta...@gmail.com, Jul 1, 2009

Yes it works. If you could make them serializable it would be perfect. You might want to add a getConsumer() method in the OAuthProvider interface for this to work.

Comment by project member m.kaepp...@gmail.com, Jul 1, 2009

alrighty, will do.

Comment by fearg...@gmail.com, Jul 2, 2009

Nice one, works for me also!

Comment by project member m.kaepp...@gmail.com, Jul 2, 2009

OAuthConsumer and OAuthProvider are now serializable.

Cheers, Matthias

Comment by pta...@gmail.com, Jul 6, 2009

Wow, that is quick. Thanks a lot for the good work.

Comment by jbasur...@gmail.com, Jul 11, 2009

Hi I got a 401 error message. Im pretty sure I'm using my own keys and it doesn't work. What could be wrong?

Fetching request token from Twitter... Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.

at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:126) at oauth.signpost.basic.DefaultOAuthProvider.retrieveRequestToken(DefaultOAuthProvider.java:66) at twittertest.Main.main(Main.java:32)
Java Result: 1

Is Twitter giving me bad keys? I also tried twitter4J and I had No luck.

Comment by project member m.kaepp...@gmail.com, Jul 12, 2009

You will get a 401 if the signatures computed by the client and the server do not match. This can happen for many reasons, but since you say it doesn't work for you anywhere anymore, it's very likely a problem with the set of credentials you're using (wrong key/secret combination maybe?). Cannot say much about this without more information.

Comment by jbasur...@gmail.com, Jul 13, 2009

Hey it worked my University (a PC with public IP btw)!. HOWEVER, when I come back home, I try running the same code and then it doesn't work again...

I mean I use THE SAME code and I used in the university (same project). I also CHANGED the keys and tried all over again with new keys and nothing! it prompts:

"Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match."

Then I remote-accessed to the University's PC, and then I ran the same code and it worked again (with the Access Tokens and even with Request Tokens!!!). It seems that the code compiles in the University but NOT at home. So must I have a Public IP in order to make it work?? I don't find the reason why it won't work at home (I need it to work at home).

I was wondering if it's something about the ports and they're blocked here. Any ideas? :(

Comment by project member m.kaepp...@gmail.com, Jul 14, 2009

That's pretty odd. It's very likely not a network problem. You will see that exception if the server answered with a 401 Unauthorized -- that happens if signature verification failed (client computed a different signature for that request than the server).

I can only guess here... but maybe it's an encoding problem (common among the OAuth camp)? You may want to send this through a debugger and see what you can find out. You may especially want to look at the signature base strings generated on the different clients and check for any peculiarities.

Comment by project member m.kaepp...@gmail.com, Jul 14, 2009

Just curious, at which point does the 401 occur? During token request or while sending the signed request? One idea I have: Are you sure that you entered the PIN code exactly as-is? It's easy to accidentally have leading or trailing white space when copy-n-pasting from a website.

Comment by jbasur...@gmail.com, Jul 14, 2009

Well, it ocurrs at: String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

it says: oauth.signpost.exception.OAuthNotAuthorizedException

I'm doing this at home, where I guess there's some kind of restriction to my IP (non-static IP). At the university, as I told you, it works just fine. So what could be happening?

Comment by phamanhv...@gmail.com, Jul 14, 2009

Hi,

I got the same issue with jbasurtod.

Fetching request token from Yammer... Request token: 0dBh4lSgf4S8u3xxxxxxx Token secret: CvM6hTZojYZX7jIKjLqvlPZBP7dgD8iw0gjxxxxxxx Now visit: https://www.yammer.com/oauth/authorize?oauth_token=0dBh4lSgf4S8u3xxxxxxx&oauth_callback=oob ... and grant this app authorization Enter the PIN code and hit ENTER when you're done: 59xx Fetching access token from Yammer... Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.

at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:126) at oauth.signpost.basic.DefaultOAuthProvider.retrieveAccessToken(DefaultOAuthProvider.java:96) at Main.main(Main.java:44)

Do you have any idea about this?

Comment by phamanhv...@gmail.com, Jul 14, 2009

Hey, actually, with the same source code, it's working with Twitter.

Fetching request token from Twitter... Request token: kQDVNkKx2qK4CAIihEFHyvgWOkeuHrO8bnPxxxxxxx Token secret: hOFzcKyUSFjs6ZEPjJ2lKlydOxC5c2b00MvWxxxxxxx Now visit: http://twitter.com/oauth/authorize?oauth_token=kQDVNkKx2qK4CAIihEFHyvgWOkeuHrO8bnPxxxxxxx ... and grant this app authorization Enter the PIN code and hit ENTER when you're done: 520xxx Fetching access token from Twitter... Access token: 42808709-LxEKZpgUq6ltPSROMYKfKth7rpamXVd34wxxxxxxx Token secret: ZlgkoVaPqN0xYHabRXXYilcB8zjFjttxxxxxxx Sending request to Twitter... Response: 200 OK

So I really don't know why it's not working with Yammer?

Comment by bost...@gmail.com, Jul 14, 2009

I'm at the final stage where I need to post an update so I do the following and getting 400 (Bad Request). App is running on Android 1.5. What am I doing wrong?

URL url = new URL("http://twitter.com/statuses/update.xml");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.addRequestProperty("status", "Hello World!");
// sign the request
consumer.sign(request);
// send the request
request.connect();
// response status should be 200 OK
int statusCode = request.getResponseCode();
Comment by phamanhv...@gmail.com, Jul 14, 2009

Hi,

I've had a test with OAuth command line Java sample and has the issue with Yammer, not Twitter. You can refer at:

http://code.google.com/p/oauth/issues/detail?id=106&colspec=ID%20Type%20Status%20Priority%20Lib%20Owner%20Summary

Comment by project member m.kaepp...@gmail.com, Jul 15, 2009

Please let's not turn this into a discussion/q&a here, especially when the problem is not related to the Twitter example (as is the case with the yammer stuff). There is a discussion forum for this: http://groups.google.com/group/signpost-users Please open a topic there.

Also, I'll do my best, but please bear in mind that I don't have the time to investigate every single problem.

Comment by basketba...@gmail.com, Jul 15, 2009

you need in android mto update the manifest to give a connection bostone can you help me with my code i can't seem to wrap my head around this. I am also using android.

Comment by basketba...@gmail.com, Jul 15, 2009

email basketballdeon@gmail.com

Comment by boogiech...@gmail.com, Jul 15, 2009

Hey guys - I extended your example to show how to do actual Twitter status update. You can find it here

Comment by talsalm...@gmail.com, Nov 12, 2009

With the latest build, you need to set oAuth10a to true (setOAuth10a(true);)

Comment by project member m.kaepp...@gmail.com, Nov 13, 2009

actually, you only need to do that when you're re-creating the OAuthProvider in the middle of a token exchange without (de)serializing it (which seems like a very uncommon scenario). Normally, that flag will be set automatically for you, based on the response structure during handshaking (token requests). The setter is only really there to reset the provider's state if you destroy it and then recreate it using 'new'.

Outside token exchange, i.e. for normal message signing, this flag is entirely meaningless.

Comment by martin.adamek, Jan 23, 2010

Guys getting 401 on Twitter, I am pretty sure your app has wrong type in Twitter setting. I fixed it by switching to 'browser' type (obviously) while running on AppEngine?.

Comment by mrdu...@gmail.com, Feb 17, 2010

I have the 401 problem with twitter during both stage.

provider.retrieveRequestToken 401 fixed by @martin.adamek recommendation.

provider.retrieveAccessToken 401 fixed by @talsalmona,recommendation to set provider.setOAuth10a(true)

Cheers!

Comment by project member m.kaepp...@gmail.com, Feb 18, 2010

Just a quick remark. If you have to manually setOAuth10a(true), then you're doing something wrong. The library automatically detects whether a provider implements 1.0 or 1.0a based on the structure of the server replies. This works with all providers I have tested, including Twitter.

In general, you indicate to the provider that you want to do an 1.0a style token exchange by always passing a non-null callback to retrieveRequestToken(). The server then automatically replies with a 1.0a style response.

As I wrote in the JavaDoc? for that method, the setOAuth10a() method is only meant to be used if you destroy and re-create a provider object between two calls to retrieve*Token().

Comment by mrdu...@gmail.com, Feb 26, 2010

You are right that I re-created the provider object between two calls. In this case, provider.setOAuth10a(true) is required for twitter.

Comment by fearg...@gmail.com, Apr 13, 2010

I suddenly started to get 401's after upgrading library versions. I had to add in provider.setOAuth10a(true) to get my login code to work again. Maybe good to add this to the example as a note for browser based two way calls.

Comment by sara.ala...@gmail.com, Jun 22, 2010

Hi

thank u for this it's really helpful

I try to do as you did in your bloa app, but when I put the callback that I set in my twitter app,it's return" page not find" in the browser.

I don't know why this error occur? do I need to set anything r do something Thanks in advance

Comment by jorgesy...@gmail.com, Jul 8, 2010

Hi Guys for the people who are gettin an "Authorization failed (server replied with a 401)." make sure the Clock is set with the right hour, it works for me...

Comment by gonzobra...@gmail.com, Nov 6, 2010

This app doesn't work for me, even after I enter my own key, secret, and the new https url's Twitter gives to me. I get this error:

Fetching request token from Twitter... Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.

at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:126) at oauth.signpost.basic.DefaultOAuthProvider.retrieveRequestToken(DefaultOAuthProvider.java:66) at Main.main(Main.java:30)

What am I doing wrong?

Comment by james.gr...@gmail.com, Jan 23, 2011

The example code needs some small changes, with the latest version of the Signpost jar.

Here's the changes I needed to work with 1.2.1.1:

--- src/Main.java.o	2009-06-21 12:27:40.000000000 +0100
+++ src/Main.java	2011-01-23 19:17:50.000000000 +0000
@@ -8,18 +8,16 @@
 import oauth.signpost.OAuthProvider;
 import oauth.signpost.basic.DefaultOAuthConsumer;
 import oauth.signpost.basic.DefaultOAuthProvider;
-import oauth.signpost.signature.SignatureMethod;
 
 public class Main {
 
     public static void main(String[] args) throws Exception {
 
         OAuthConsumer consumer = new DefaultOAuthConsumer(
                "iIlNngv1KdV6XzNYkoLA",
-                "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM",
-                SignatureMethod.HMAC_SHA1);
+	 	 "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM");
 
-        OAuthProvider provider = new DefaultOAuthProvider(consumer,
+        OAuthProvider provider = new DefaultOAuthProvider(
                 "http://twitter.com/oauth/request_token",
                 "http://twitter.com/oauth/access_token",
                 "http://twitter.com/oauth/authorize");
@@ -27,7 +25,8 @@
         System.out.println("Fetching request token from Twitter...");
 
         // we do not support callbacks, thus pass OOB
-        String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);
+        String authUrl = provider.retrieveRequestToken(consumer,
+		OAuth.OUT_OF_BAND);
 
         System.out.println("Request token: " + consumer.getToken());
         System.out.println("Token secret: " + consumer.getTokenSecret());
@@ -41,7 +40,7 @@
 
         System.out.println("Fetching access token from Twitter...");
 
-        provider.retrieveAccessToken(pin);
+        provider.retrieveAccessToken(consumer,pin);
 
         System.out.println("Access token: " + consumer.getToken());
         System.out.println("Token secret: " + consumer.getTokenSecret());
Comment by sadco...@gmail.com, Apr 29, 2011

how can i open this jar james? and what is first line and second line of your code described?

Comment by leanh...@gmail.com, Feb 27, 2012

I got this error in example. How can I solve ? Fetching request token from Twitter... Exception in thread "main" oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: java.security.NoSuchAlgorithmException?: Algorithm HmacSHA1 not available

at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:150) at oauth.signpost.basic.DefaultOAuthProvider.retrieveRequestToken(DefaultOAuthProvider.java:66) at Main.main(Main.java:38)

Comment by daniel.winterstein, Nov 4, 2012

Looking at some of the issues & questions people have, I'd like to recommend JTwitter would make your life easier. Disclaimer: I wrote JTwitter; but I make absolutely no money from it - I'm recommending it because IMHO it's genuinely useful. JTwitter is an open-source (LGPL) Twitter client that uses Signpost, but adds a lot of Twitter convenience & robustness:

http://www.winterwell.com/software/jtwitter.php


Sign in to add a comment
Powered by Google Project Hosting