Results 1 to 2 of 2

Thread: Problem posting tweets with TwitterTemplate

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    1

    Default Problem posting tweets with TwitterTemplate

    For a project we're trying to use spring social to post twitter and facebook updates.

    I'm using a TwitterTemplate for communication with twitter like so:

    Code:
    public boolean postTweet(String pAccessToken, String pAccessTokenSecret, String pTweet){
    		TwitterTemplate tTwitter = new TwitterTemplate(fTwitterApiKey, fTwitterApiSecret, pAccessToken, pAccessTokenSecret);
    
    		if(pTweet == null || "".equals(pTweet))
    			throw new IllegalArgumentException("Tweet is empty");
    		
    		try{
    			tTwitter.updateStatus(pTweet);
    		}
    		catch(SocialException e){
    			return false;
    		}
    		
    		return true;
    	}
    For the FacebookTemplate, this approach works perfectly. I keep on struggling with the TwitterTemplate. The accesstoken and accestokensecret are obtained by the OAuth dance client side and stored by the backend.

    When I try to post a tweet I get the following exception:
    HTML Code:
    org.springframework.social.AccountNotConnectedException: Incorrect signature
    I know requests to the twitter api should be signed, but my expectation was for spring social to handle that. Also, I fail to see how I could supply the TwitterTemplate with a signature.

  2. #2
    Join Date
    Aug 2004
    Posts
    1,075

    Default

    As you're probably aware, Facebook's authorization is based on OAuth 2 which is dramatically simpler. Give FacebookTemplate a valid access token and it will probably work. Twitter's authorization is based on OAuth 1.0a, which is quite some bit more complex. If you get that signature wrong, then the request won't work and you'll get the exception you see.

    That said, TwitterTemplate does handle the details of signing the request for you. As long as you're giving it a valid consumer key, consumer secret, access token, and access token secret (and the access token/secret were obtained using the same consumer key and secret), then I can't imagine why it wouldn't work.

    You say that you're gathering the access token and secret yourself, storing them in the backend and then (as your code shows) instantiating TwitterTemplate yourself. I wonder (1) if somewhere between obtaining the token/secret and constructing TwitterTemplate the token and/or secret were somehow changed...perhaps encrypted or encoded in some way that makes them invalid...

    ...and (2) have you had a look at the service provider connection framework in Spring Social 1.0.0.M2? It handles all of that token handling for you. In fact, your code should never need to touch an access token directly. Once a connection is created between your user's local account and their Twitter account, you can use that connection to get a ready-to-go TwitterTemplate. In case you missed it, I posted a blog about this a few days ago at http://blog.springsource.com/2011/03...der-framework/.
    Craig Walls
    Spring Social Project Lead

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •