title: How to leverage request-promise to request OAuth2 token from Oauth server and include the token in request headers
body: <p>Disclaimer: I'm very new Node developer and pretty new to javascript also. I have a lot of experience with Java, C++, and frameworks around those, so I understand development paradigms, but I'm not that familiar with how JS is structured or design patterns yet. And I'm in a bit of a pickle to get this done quickly :/</p>
  
  <p>Our node application needs to make requests to services that require OAuth2 authentication. </p>
  
  <p>Our app can successfully request and receive an OAuth bearer token, and it also can make requests to services using that token. But its all very decoupled.</p>
  
  <p>What I'd like to do is hook up an intercepter to the request module (we are using request-promise) that is called as such:</p>
  
  <ol>
  <li>The request module passes control to us prior to calling the requested
  service </li>
  <li>We get our cached copy of the oauth token headers OR request a new token and build the headers (and cache it)</li>
  <li>We insert the new oauth token headers to the request </li>
  <li>Then return control to the request module for normal execution</li>
  </ol>
  
  <p>I looked at request-promise and see that it's doing something similar in terms of hooking onto the 'request' module. And looking at the nodejs 'request' module, it looks like there's possibly a hook for adding OAuth headers.</p>
  
  <p>My question is how to I leverage the hook if it exists. And is this the best approach? Finally, is there an example/pattern for this specific case. I see lots of examples for OAuth in node, but most of it is for authenticating an incoming client, not an outgoing request, and the examples I find have a very decoupled process similar to what we already have.</p>
  
  <p>Many thanks in advance for any assistance on this.</p>