Results 1 to 9 of 9

Thread: getting OAuth2ClientContext

  1. #1
    Join Date
    Aug 2012
    Posts
    104

    Question getting OAuth2ClientContext

    Hei Dave,

    Trying to upgrade from M6 to RC2a (it's about time ), I've encountered a challenge: in M6, in the client, when I wanted to get the accessToken, I did the following:

    Code:
    OAuth2AccessToken token = OAuth2ClientContextHolder.getContext().getAccessToken(resource);
    in RC2a I see that OAuth2ClientContextHolder was removed. So how can I get the OAuth2ClientContext in RC2a?

  2. #2
    Join Date
    Jun 2005
    Posts
    4,241

    Default

    The general idea is that you shouldn't really need to know the access token value (it is opaque to a client anyway). But if you really need it you can get it from the OAuth2RestTemplate through the OAuth2RestOperations interface.

  3. #3
    Join Date
    Aug 2012
    Posts
    104

    Default

    ...and what is the best way to get the OAuth2RestTemplate ? I guess from the bean that invokes the REST call and uses <oauth:rest-template>?

  4. #4
    Join Date
    Jun 2005
    Posts
    4,241

    Default

    This is dependency injection. You can give the <oauth:template/> an id and use it anywhere you want.

  5. #5
    Join Date
    Aug 2012
    Posts
    104

    Question

    not sure how to do this ...
    Is this what you mean?

    Code:
    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-security.xml");
    MyThing thing = appContext.getBean(MyThing.class);
    OAuth2RestTemplate restTemplate = thing.getClientRestTemplate();
    OAuth2AccessToken token2 = restTemplate.getAccessToken();
    I have this bean in my XML:
    Code:
    <bean id="watchdoxApiService" class="com.watchdox.web.auth.MyThing">
    	<property name="clientRestTemplate">
    		<oauth:rest-template resource="myApiResource" />
    	</property>
    
    </bean>

  6. #6
    Join Date
    Aug 2012
    Posts
    104

    Default

    Oh, I think you mean
    Code:
    <bean ...>
    	<property name="clientRestTemplate">
    		<oauth:rest-template id="theId" resource="myApiResource" />
    	</property>
    </bean>
    and then
    Code:
    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-security.xml");
    OAuth2RestTemplate restTemplate =  appContext.getBean("theId");
    OAuth2AccessToken token = restTemplate.getAccessToken();
    Am I right?
    thanks!

  7. #7
    Join Date
    Jun 2005
    Posts
    4,241

    Default

    Not really. That's dependency lookup. Effective but ugly, so go ahead if that works for you. This is Spring 101, so if you want to find out more ingeneral about Spring and DI, head over to the core forum, or stackoverflow.

  8. #8
    Join Date
    Aug 2012
    Posts
    104

    Default

    Quote Originally Posted by Dave Syer View Post
    Not really. That's dependency lookup. Effective but ugly, so go ahead if that works for you. This is Spring 101, so if you want to find out more ingeneral about Spring and DI, head over to the core forum, or stackoverflow.
    your "no" refers to my first snippet (7:08AM) or to the 2nd one (7:16AM)? both are wrong?
    thanks

  9. #9
    Join Date
    Jun 2005
    Posts
    4,241

    Default

    I'm not sure either will work because your rest-template is an inner bean. You probably need to put it up at the top level, give it an id, and preferably inject it into the component where you need it (not look it up from the context).

Posting Permissions

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