title: How do I extract the Oauth2 access token in a Spring application using code grant flow?
body: <p>I have followed <a href="https://spring.io/guides/tutorials/spring-boot-oauth2/" rel="nofollow noreferrer">this guide.</a></p>
  <p>I have a simple Oauth2 webapp using code grant authorization flow. I have a @GetMapping endpoint (as per the tutorial) that returns user information retrieved from a service provider I'm using. Since this correctly returns my information to the webapp, I can tell the code grant flow has worked. Code for working endpoint:</p>
  <pre><code>@GetMapping(&quot;/user&quot;)
  public Map&lt;String, Object&gt; user(@AuthenticationPrincipal OAuth2User principal) {
      return principal.getAttributes();
  }
  </code></pre>
  <p>Now I want to create a new @GetMapping where I use the access token to query the API of the service provider that just authorized us.</p>
  <p>The problem is that the example above somehow magically makes the request for me based solely on configuration and only returns user info. Now I want to get the access token for this session that Spring stores somewhere to access the API however I like &quot;manually&quot;.</p>
  <p>How can I extract the access token in my new @GetMapping?</p>
  <p>I have been searching a lot, but I am new to both Oauth2 and Spring, and there is so much spring classes and tutorials that I &quot;can't see the forest because of all the trees&quot;.</p>
  <p>I appriciate any help.
  Cheers.</p>