Announcement Announcement Module
Collapse
No announcement yet.
getting OAuth2ClientContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      ...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>?

      Comment


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

        Comment


        • #5
          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>

          Comment


          • #6
            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!

            Comment


            • #7
              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.

              Comment


              • #8
                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

                Comment


                • #9
                  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).

                  Comment

                  Working...
                  X