Announcement Announcement Module
Collapse
No announcement yet.
How to configure OAuth2RestTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to configure OAuth2RestTemplate

    Updated

    I am not sure if I have OAuth2RestTemplate configured correctly.

    Error:
    Code:
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1df3248: defining beans [propertyConfigurer,dataSource,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,emf,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,accountRepository,questionRepository,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,jpaQuestionService,jpaAccountService,passwordEncoder,accountHelper,tradeConfig,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#1,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,baseOAuth2ProtectedResourceDetails,oAuth2ProtectedResourceDetails,accessTokenRequest,oAuth2ClientContext,oAuth2RestTemplate]; root of factory hierarchy
    Exception in thread "main" error="access_denied", error_description="Unable to obtain a new access token for resource 'null'. The provider manager is not configured to support it."
    	at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:146)
    	at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
    	at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:216)
    	at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:168)
    	at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:89)
    	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:442)
    	at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:123)
    	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)
    	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385)
    	at com.Tester.main(TraderTester.java:44)
    Can somebody take a look and give some feedback?
    Code:
        @Configuration
    public class AppConfig {
    
            //@Scope(value="singleton", proxyMode=ScopedProxyMode.INTERFACES) <-- Why do I get an error if I uncomment this line?
            @Bean
            public BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails(){
                    BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails =  new BaseOAuth2ProtectedResourceDetails();
                    baseOAuth2ProtectedResourceDetails.setClientId(clientId);
                    baseOAuth2ProtectedResourceDetails.setClientSecret(clientSecret);
                    return baseOAuth2ProtectedResourceDetails;
            }
    
           	@Bean
    	public DefaultAccessTokenRequest accessTokenRequest(){
    		return new DefaultAccessTokenRequest();
    	}
    	
    	@Bean
    	public OAuth2ClientContext oAuth2ClientContext(){
    		return new DefaultOAuth2ClientContext(accessTokenRequest());
    	}
    	
    	@Bean
    	public OAuth2RestTemplate oAuth2RestTemplate(){
    		OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(baseOAuth2ProtectedResourceDetails(),oAuth2ClientContext());
    		return restTemplate;
    	}
    }}
    
    Tester Class
    
    
    public static void main(String[] args) {
    
                    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
                    ctx.load("classpath*:jpa-app-context.xml");
                    ctx.refresh();
    
            EntityManagerFactory emf = (EntityManagerFactory) ctx.getBean("emf");
            EntityManager em = emf.createEntityManager();
            TransactionSynchronizationManager.bindResource(emf , new EntityManagerHolder(em));
    OAuth2RestTemplate oAuth2RestTemplate = (OAuth2RestTemplate) ctx.getBean("oAuth2RestTemplate");
         String uri="https://api..";
            Object obj = oAuth2RestTemplate.exchange(uri, HttpMethod.POST, null, Object.class);
            System.out.println("Tester Object: "+ obj.toString());
    
    
    }
    }
    Last edited by Laedislaw; Feb 2nd, 2013, 10:46 AM.

  • #2
    You haven't specified a grant_type - normally you do that by selecting a subclass of BaseOAuth2ProtectedResourceDetails. Also, you wouldn't normally see static @Bean definitions for the AccessTokenRequest and OAuth2ClientContext (without @Scope) unless you were trying to use grant_type=client_credentials, so I'm guessing you meant to use ClientCredentialsResourceDetails.

    Comment


    • #3
      Not sure, but I think oAuth2RestTemplate can't be singletone... rather session or request scope

      (bad documented area... big minus to Spring )

      Comment


      • #4
        For client credentials grant it makes sense to have a singleton RestTemplate. If you don't like the documentation, it's open source - in fact you don't even need a pull request right now because it's a GH wiki and you can edit it if you are a github user.

        Comment


        • #5
          Yep, I like such answers
          If you don't like our bad documentation - try to understand everything and write your own
          +1

          Comment

          Working...
          X