Announcement Announcement Module
Collapse
No announcement yet.
Getting Twitter Authentication error. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting Twitter Authentication error.

    So I just wrote some code to use JMS Queue to send a String message which is then used to back a Spring Integration channel, which is being listened on my an <int-twitter:outbound-channel-adapter> which references my TwitterTemplate bean and therefore sending the JMS Message should cause a status update to my account.

    However, I am getting an error.
    First a warning
    "2012-03-17 18:55:29,575 [http-bio-8080-exec-1] WARN org.springframework.web.client.RestTemplate - POST request for "https://api.twitter.com/1/statuses/update.json" resulted in 401 (Unauthorized); invoking error handler"

    Then the error, the stack trace has more, but nothing worth noting.

    Code:
    SEVERE: Servlet.service() for servlet [eventgate] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error: cannot retry due to server authentication, in streaming mode; nested exception is java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode] with root cause
    java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
    	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1257)
    	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
    	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
    	at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:48)
    	at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415)
    	at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:294)
    	at org.springframework.social.twitter.api.impl.TimelineTemplate.updateStatus(TimelineTemplate.java:236)
    	at org.springframework.social.twitter.api.impl.TimelineTemplate.updateStatus(TimelineTemplate.java:224)
    	at com.perfectworldprogramming.eventgate.utils.TwitterService.sendTweet(TwitterService.java:58)
    Here is my code and configuration

    Code:
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="vm://embedded?broker.persistent=false"/>
        </bean>
    
        <bean id="twitterQueue" class="org.apache.activemq.command.ActiveMQQueue">
            <constructor-arg value="queue.twitter"/>
        </bean>
        
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory" ref="connectionFactory"/>
            <property name="defaultDestination" ref="twitterQueue"/>
        </bean>
    
    <bean id="twitterTemplate" class="org.springframework.social.twitter.api.impl.TwitterTemplate">
        	<constructor-arg value="${twitter.oauth.consumerKey}"/>
        	<constructor-arg value="${twitter.oauth.consumerSecret}"/>
        	<constructor-arg value="${twitter.oauth.accessToken}"/>
        	<constructor-arg value="${twitter.oauth.accessTokenSecret}"/>
        </bean>
        
        <int-twitter:outbound-channel-adapter
          		twitter-template="twitterTemplate"
          		channel="twitterChannel"/>
    
        <int-jms:channel id="twitterChannel" queue-name="queue.twitter"/>
    Code

    Code:
        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public void sendTweet(TwitterMessage messageType, String... args) {
            String message = messages.get(messageType);
            message = MessageFormat.format(message, args) + Calendar.getInstance().toString();
            if (message.length() > tweetSize) {
                message = message.substring(0, 139);
            }
            boolean testing = twitterTemplate.isAuthorized();
            if (testing) {
                jmsTemplate.convertAndSend(message);
            }
    
        }
    It looks correct to me, and in my .properties file with my Twitter credentials all seems fine, I even get true returned for twitterTemplate.isAuthorized();

    Any help would be appreciated.

    Thanks

    Mark

  • #2
    Well judging by the stack trace, you clearly have an issue with you credentials. I would suggest to try to post a status directly using SpringSocial to verify.
    However since you are already using SpringIntegration you might as well use our Twitter module especially for the use case as simple as yours. Here are some samples https://github.com/SpringSource/spri.../basic/twitter

    Comment


    • #3
      Ohh wait, i'll take that back.I see you are using SI for this. Sorry about it.

      Did it ever work for you? If so there could be two issues - both related to Twitter itself. 1. You might be a subject of rate limiting (posting too many messages in the certain timeframe); 2. You can't post status with the same content more than once.

      Can you confirm?

      Comment


      • #4
        Thanks Oleg. Confirmed. I changed the message to have a timestamp so it would be different each time. I only tried about 10 times total till I gave up and posted.

        I have an idea, I am using a Mac with Safari and I always have privacy browsing turned on. Let me turn that off and try to send one.

        Thanks

        Mark

        Comment


        • #5
          Nope, not privacy browsing. I am looking at
          http://forum.springsource.org/showth...ost-to-twitter

          In which Craig has some interesting points.

          and the link by the other guy in that thread

          http://stackoverflow.com/questions/3...ponse-code-401

          About to try them out. I have a good feeling about the changing it from client to browser, since the call starts from a URL link on my website.

          Thanks again Oleg

          Mark

          p.s. I think we should just ignore that "article" BB posted about JBoss 7. It is obvious from the text that he doesn't know anything about what Spring has and is just thinking Spring is just a container/DI and nothing more. At this point it is like he is being a troll.

          Comment


          • #6
            Still, no luck.

            However, I think Twitter is having issues with their dev.twitter.com site. It keeps popping up a message about being "over capacity"

            Anyway, thanks for your help. I need to go onto other stuff for the next week, so I will have to come back to this much later.

            Mark

            Comment


            • #7
              Yep, it was a twitter problem. Couldn't get it to change my oauth token keys from read to read/write. I kept clicking the read/write radio button and got that over capacity error. What I finally did to get it to work was after clicking the read/write radio button then submitting and getting the capacity error message, I then clicked on the regenerate keys button for the tokens, and when I did that I got the new keys and it then also showed as read/write on the dev.twitter.com web site.

              Copied the new keys to my properties file and now I am tweeting like crazy through my app.

              Thanks Oleg for all your help, you are always there to hellp us all out. Next Spring One I'm buying you a beer.
              Mark

              Comment


              • #8
                i haved the same problem,i solved making an account here:
                https://dev.twitter.com/apps/new
                (in "access token" put the: Access level = Read and write)

                them just follow the API: http://static.springsource.org/sprin...html/apis.html
                and made an example step by step.

                Dayi,

                Comment


                • #9
                  Please refer my blog where I posted an example for Twitter Integration - Post a Tweet, Receive a Tweet

                  The first example deals with the steps you would need to authenticate and authorize your application with twitter.

                  Hope that helps!

                  Comment

                  Working...
                  X