title: Play Framework: getting foursquare access token
body: <p>I am trying to grasp how Play framework handles its web service calls.</p>
  
  <p>This is Foursquare's directions (after 1 and 2):</p>
  
  <pre><code>Your server will make a request for
  https://foursquare.com/oauth2/access_token
      ?client_id=YOUR_CLIENT_ID
      &amp;client_secret=YOUR_CLIENT_SECRET
      &amp;grant_type=authorization_code
      &amp;redirect_uri=YOUR_REGISTERED_REDIRECT_URI
      &amp;code=CODE
  
  The response will be JSON
  { access_token: ACCESS_TOKEN }
  </code></pre>
  
  <p>And this is my code:</p>
  
  <pre><code>public static Result index() {
      String base = "http://foursquare.com/oauth2/authenticate";
      String clientId = "?client_id=***";
      String responseType = "&amp;response_type=code";
      String redirectUri = "&amp;redirect_uri=http://localhost:9000/code";
      return redirect(base + clientId + responseType + redirectUri);
  }
  
  
  public static Result code() {
          String base = "http://foursquare.com/oauth2/access_token";
          String clientId = "?client_id=***";
          String clientsecret = "&amp;client_secret=***";
          String grantType = "&amp;grant_type=authorization_code";
          String redirectUri = "&amp;redirect_uri=http://localhost:9000/json";
          String code = "&amp;code=" + request().getQueryString("code");
  
          return redirect(base + clientId + clientsecret + grantType
                  + redirectUri + code);
  
      }
  </code></pre>
  
  <p>There are two things I am curios about:</p>
  
  <p>1) I am redirected to a page with the access token displayed on screen:</p>
  
  <p>URL: <code>https://foursquare.com/oauth2/access_token?client_id=***&amp;client_secret=***&amp;grant_type=authorization_code&amp;redirect_uri=http://localhost:9000/json&amp;code=***</code></p>
  
  <p>With this displayed:</p>
  
  <pre><code>{"access_token":"***"}
  </code></pre>
  
  <p>I would have expected to be redirected to <code>http://localhost:9000/json</code> as that was the redirect uri...</p>
  
  <p>2) How do I grab the access token? I have tried using the asynchronous web service calls vis <a href="http://www.playframework.com/documentation/2.2.x/JavaWS" rel="nofollow">http://www.playframework.com/documentation/2.2.x/JavaWS</a> but I cannot make them work.</p>
  
  <p>My attempt: </p>
  
  <pre><code>String feedUrl = base + clientId + clientsecret + grantType
                  + redirectUri + code;
  
          final Promise&lt;Result&gt; resultPromise = WS.url(feedUrl).get().map(
                  new Function&lt;WS.Response, Result&gt;() {
                      public Result apply(WS.Response response) {
                          return ok("Feed title:" + response.asJson().findPath("access_token").asText());
                      }
                  }
          );
  
          return resultPromise;
  </code></pre>
  
  <p>This gives me an exception: <code>[ConnectException: Connection refused: no further information: foursquare.com/107.23.103.139:80 to http://foursquare.com/oauth2/access_token]</code></p>
  
  <p>All help is appreciated greatly!</p>