String path = request.getPathInfo().replace("/", "");
if (OAuthRealm.LOG.isTraceEnabled())
OAuthRealm.LOG.trace("the " + request.getMethod() + " method, path info "+path);
OAuthService service =
OAuthRealm._.getServiceBulderByPath(path)
.getServiceBuilder()
.callback(request.getRequestURL().toString())
.build();
if (request.getParameterMap().containsKey(RETURN_TO_PAGE)) {
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
request.getSession().setAttribute(RETURN_TO_PAGE, request.getParameter(RETURN_TO_PAGE));
response.sendRedirect(authorizationUrl);
return;
}
String verification = request.getParameter("code");
Verifier verifier = new Verifier(verification);
Token accessToken = null;
//workaround google API
if (OAuthRealm._.getServiceBulderByPath(path).provider.equalsIgnoreCase("google")) {
Google2Api api = new Google2Api();
Service config = OAuthRealm._.getServiceBulderByPath(path);
OAuthRequest req = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
req.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
req.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
req.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
// jetty.port.jetty
req.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getReturnURL());
req.addBodyParameter("grant_type", "authorization_code");
String responce = req.send().getBody();
JSONTokener tokener = new JSONTokener(responce);
try {
JSONObject root = new JSONObject(tokener);
String access_token = root.getString("access_token");
String token = OAuthEncoder.decode(access_token);
accessToken = new Token(token, "", responce);
} catch (JSONException e) {
throw new IOException(e);
}
//accessToken = api.getAccessTokenExtractor().extract(req.send().getBody());
} else
accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
try {
OAuthRealm._.getServiceBulderByPath(path).saveAccessToken(request, service, accessToken);
} catch (Exception e) {
throw new ServletException(e);