params.put("client_id", clientid);
params.put("client_secret", secret);
params.put("redirect_uri",
play.mvc.Router.getFullUrl("ForceDotComOAuth2.callback"));
params.put("code", accessCode);
HttpResponse response = WS.url(sandboxLogin ? TOKEN_URL_sBox : TOKEN_URL).params(params).post();
JsonObject r = response.getJson().getAsJsonObject();
// ensure all expected elements are present in response object
if (attributesPresent(r, Lists.newArrayList(ACCESS_TOKEN,
REFRESH_TOKEN, ID_ATTR, INSTANCE_URL, SIGNATURE))) {
OAuthSession s = new OAuthSession();
s.access_token = r.getAsJsonPrimitive(ACCESS_TOKEN).getAsString();
s.refresh_token = r.getAsJsonPrimitive(REFRESH_TOKEN).getAsString();
s.idURL = r.getAsJsonPrimitive(ID_ATTR).getAsString();
s.instance_url = r.getAsJsonPrimitive(INSTANCE_URL).getAsString();
s.signature = r.getAsJsonPrimitive(SIGNATURE).getAsString();
String id = s.idURL.substring(s.idURL.lastIndexOf('/') + 1);
s.uid = id;
Cache.set(session.getId() + "-oauth", s);
Cache.set(session.getId() + "-sandbox", sandboxLogin);
if (isPersistentSession()) {
Response.current().setCookie("uid",
id + "-" + Crypto.sign(id), "30d");
s.save();
}
listener.onSuccess(s);
} else {
Logger.error("Callback failed. HttpResponse was not valid after Post for token." +
(sandboxLogin ? "Sandbox login attempt." : "Prod login attempt."));
Logger.error("Response body from post: %s", response.getString());
JsonPrimitive error = r.getAsJsonPrimitive("error");
JsonPrimitive errorDescription = r.getAsJsonPrimitive("error_description");
if (error != null && errorDescription != null) {
listener.onFailure(error.getAsString(), errorDescription.getAsString());