Results 1 to 5 of 5

Thread: How to configure OAuth2RestTemplate

Threaded View

  1. #1

    Default 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 at 10:46 AM.

Tags for this Thread

Posting Permissions

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