Announcement Announcement Module
Collapse
No announcement yet.
Spring OAuth noob - OAuth dance with twitter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring OAuth noob - OAuth dance with twitter

    Hi,

    I haven't bean using spring for very long, but I'm trying to use spring oauth to get an access token from twitter.

    I'm trying to get the token in a controller simply without using spring security to protect an endpoint.

    When I call getUnauthorizedRequestToken it throws a NullPointerException.

    I have the ProtectedResourceDetailsService and the ProtectedResourceDetails set up in my servlet-context.xml like so.

    Code:
    <beans:bean id="protectedResourceDetailsService" class="org.springframework.security.oauth.consumer.InMemoryProtectedResourceDetailsService">
            <beans:property name="resourceDetailsStore">
                <beans:map>
                    <beans:entry key="twitter" value-ref="twitterResources"/>
                </beans:map>
            </beans:property>
        </beans:bean>
    
        <beans:bean id="twitterResources" class="org.springframework.security.oauth.consumer.BaseProtectedResourceDetails">
            <beans:property name="id" value="twitter"/>
            <beans:property name="consumerKey" value="abc"/>
            <beans:property name="sharedSecret">
                <beans:bean class="org.springframework.security.oauth.common.signature.SharedConsumerSecret">
                    <beans:constructor-arg value="xyz"/>
                </beans:bean>
            </beans:property>
            <beans:property name="requestTokenURL" value="http://api.twitter.com/oauth/request_token"/>
            <beans:property name="accessTokenURL" value="http://api.twitter.com/oauth/access_token"/>
            <beans:property name="userAuthorizationURL" value="http://api.twitter.com/oauth/authorize"/>
            <beans:property name="signatureMethod" value="HMAC-SHA1"/>
            <beans:property name="use10a" value="true"/>
        </beans:bean>
    ... and my controller looks like this :

    Code:
    @Controller
    @RequestMapping(value= "/twitter")
    public class TwitterController
    {
        @Autowired
        private InMemoryProtectedResourceDetailsService protectedResourceDetailsService;
    
        private static final Logger logger= LoggerFactory.getLogger(TwitterController.class);
    
        private CoreOAuthConsumerSupport consumerSupport = new CoreOAuthConsumerSupport();
        
        
        @RequestMapping(value= "/authenticate", method= RequestMethod.GET)
        public String authenticate ()
        {
            
            consumerSupport.setProtectedResourceDetailsService( protectedResourceDetailsService ) ;
            
            try
            {
                /*
                ProtectedResourceDetails twitter = protectedResourceDetailsService.loadProtectedResourceDetailsById("twitter");
                logger.info(twitter.getRequestTokenURL());
                logger.info(twitter.getRequestTokenHttpMethod());
                */
    
                consumerSupport.getUnauthorizedRequestToken("twitter", "http://example.com");
                
            }
            catch (Exception e)
            {
                logger.error(e.getMessage(), e);
            }
            return "home";
        }
    
        
    }
    However when I try and call getUnauthorizedRequestToken() it throws a NullPointerException.

    stack trace :

    Code:
    java.lang.NullPointerException
    	at org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport.configureURLForProtectedAccess(CoreOAuthConsumerSupport.java:268)
    	at org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport.readResource(CoreOAuthConsumerSupport.java:154)
    	at org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport.getTokenFromProvider(CoreOAuthConsumerSupport.java:400)
    	at org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport.getUnauthorizedRequestToken(CoreOAuthConsumerSupport.java:96)
    	at com.sinkingfish.oauth.twitter.TwitterController.authenticate(TwitterController.java:39)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.traceNextValve(HttpRequestOperationCollectionValve.java:92)
    	at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:74)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:409)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:680)
    Am I going about this all wrong? I haven't been able to find a solid example of doing simple twitter access without using spring security for authorisation & authentication.

    All advice welcome

    Brian
    Last edited by sinkingfish; Feb 24th, 2011, 08:58 AM.

  • #2
    Looks like you need to set up a stream handler factory on the ConsumerSupport bean.

    After this line:

    Code:
    consumerSupport.setProtectedResourceDetailsService( protectedResourceDetailsService ) ;
    Do this:

    Code:
    consumerSupport.setStreamHandlerFactory(new org.springframework.security.oauth.consumer.net.DefaultOAuthURLStreamHandlerFactory());
    Normally, this would happen during bean initialization because the afterPropertiesSet() method gets called on the ConsumerSupport object. But since you're handling the lifecycle of the ConsumerSupport bean, you have to wire up its dependencies by hand.

    Comment


    • #3
      btw have you seen http://www.springsource.org/spring-social
      it might help with twitter

      Comment

      Working...
      X