title: correct way Mule OAuth2 client credentials refresh token
body: <p>I am using mule to connect/consume some services. Authentication is OAuth2 client-credential.
  What is best way to refresh the token?
  One way could be checking http.status like </p>
  
  <pre><code>refreshTokenWhen="#[message.inboundProperties['http.status'] == 401]"
  </code></pre>
  
  <p>but I am not very happy with that since it should fail once to refresh the token. 
  Is there anyway to refresh the token based on expire time?
  my sample code:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2" xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:spring="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
  http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
  http://www.mulesoft.org/schema/mule/oauth2 http://www.mulesoft.org/schema/mule/oauth2/current/mule-oauth2.xsd
  http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
  http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd"&gt;
  &lt;http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="${remote.host}" port="${remote.port}" basePath="${remote.path}" doc:name="HTTP Request Configuration"&gt;
      &lt;oauth2:client-credentials-grant-type clientId="${clientid}" clientSecret="${clientSecret}" tokenManager-ref="Token_Manager_Config"&gt;
          &lt;oauth2:token-request tokenUrl="${remote.tokenUrl}" refreshTokenWhen="#[message.inboundProperties['http.status'] == 401 ]"&gt;
              &lt;oauth2:token-response accessToken="#[json:access_token]" expiresIn="#[json:expires_in]"/&gt;
          &lt;/oauth2:token-request&gt;
      &lt;/oauth2:client-credentials-grant-type&gt;
  &lt;/http:request-config&gt;
  &lt;oauth2:token-manager-config name="Token_Manager_Config" doc:name="Token Manager Config"/&gt;
  </code></pre>