title: How to use a single OAuth2.0 token for Multiple Virtual Users in a Gatling load test
body: <p>I need to load test an API that requires an OAuth2.0 token via Gatling (of which I'm a complete novice!) but would like each virtual user to use the same token. I'm retrieving the token ok (I think) and putting it in a variable called 'access' but I keep getting 'no attribute named 'access' is defined' when the test itself starts.</p>
  
  <p>My token retrieval looks like the following(along with httpConf, used below):</p>
  
  <pre><code> class MySimulation extends Simulation {
  
   val httpConf = http        
      .baseUrl("https://MyBaseUrl.Com/")
      .acceptHeader("application/json") 
      .doNotTrackHeader("1")
      .acceptLanguageHeader("en-UK,en;q=0.5")
      .acceptEncodingHeader("gzip, deflate")
      .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
      .shareConnections
  
   val header = Map("Content-Type" -&gt; """application/x-www-form-urlencoded""")
  
   al auth = scenario("Retrieve Token")
   .exec(http("POST OAuth Req")
   .post("https://SomeTokenUrl")
   .formParam("resource", "someresource")
   .formParam("grant_type", "somegranttype")
   .formParam("client_secret", "someclientsecret")
   .formParam("client_id", "someclientid")
   .headers(header).check(status.is(200)).check(jsonPath("$.access_token").find.saveAs("access"))) 
  </code></pre>
  
  <p>I then tried setting up the load test as (Note: I did initially put 'Map', rather than the mutable variant, but read somewhere the default was immutable, and wondered if this was why the header couldn't update):</p>
  
  <pre><code> val headers_10 = scala.collection.mutable.Map("Content-Type" -&gt; "application/json; charset=ISO-8859-1", "Authorization" -&gt; "Bearer ${access}")
  
   val scn = scenario("MyService Gatling test run")       
             .exec(http("")               
             .post("Myservice/api")
             .headers(headers_10.toMap)                
             .body(StringBody("""{"SomeProperty": "Some Value"}"""))
             .asJson
             .check(status.is(200)))
  
   setUp(
      auth.inject(constantUsersPerSec(1) during (2 seconds)),
      scn.inject(nothingFor(2 seconds),
      constantUsersPerSec(10) during (10 seconds) 
      ).protocols(httpConf))
      .assertions(global.responseTime.max.lt(500)) 
      .assertions(forAll.failedRequests.percent.lte(1)) 
      .assertions(global.responseTime.mean.lte(100)) 
  </code></pre>
  
  <p>The idea was that the token retrieval would complete prior to the load test kicking in and the 'access' variable would then be used by the load test scenario, but it gives the following result:</p>
  
  <pre><code>  ERROR : Failed to build request: No attribute named 'access' is defined 
  </code></pre>
  
  <p>I've reached the end of my tether with it.  I'm guessing it might be something to do with scopes, and perhaps the variable doesn't carry over to the load test scenario, but I've seen examples elsewhere that seem to recommend exactly that set up, so I don't know whether these other examples are partially complete or what.</p>