use of com.github.scribejava.core.model.Response in project scribejava by scribejava.
the class OAuth10aService method getRequestToken.
public OAuth1RequestToken getRequestToken() throws IOException, InterruptedException, ExecutionException {
final OAuthConfig config = getConfig();
config.log("obtaining request token from " + api.getRequestTokenEndpoint());
final OAuthRequest request = prepareRequestTokenRequest();
config.log("sending request...");
final Response response = execute(request);
final String body = response.getBody();
config.log("response status code: " + response.getCode());
config.log("response body: " + body);
return api.getRequestTokenExtractor().extract(response);
}
use of com.github.scribejava.core.model.Response in project scribejava by scribejava.
the class AbstractClientTest method shouldSendGetRequest.
@Test
public void shouldSendGetRequest() throws Exception {
final String expectedResponseBody = "response body for test shouldSendGetRequest";
final MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody(expectedResponseBody));
server.start();
final HttpUrl baseUrl = server.url("/testUrl");
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
assertEquals(expectedResponseBody, response.getBody());
final RecordedRequest recordedRequest = server.takeRequest();
assertEquals("GET", recordedRequest.getMethod());
server.shutdown();
}
use of com.github.scribejava.core.model.Response in project scribejava by scribejava.
the class JDKHttpClient method executeAsync.
@Override
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, String bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
try {
final Response response = execute(userAgent, headers, httpVerb, completeUrl, bodyContents);
@SuppressWarnings("unchecked") final T t = converter == null ? (T) response : converter.convert(response);
if (callback != null) {
callback.onCompleted(t);
}
return new JDKHttpFuture<>(t);
} catch (InterruptedException | ExecutionException | IOException e) {
if (callback != null) {
callback.onThrowable(e);
}
return new JDKHttpFuture<>(e);
}
}
use of com.github.scribejava.core.model.Response in project scribejava by scribejava.
the class JDKHttpClient method executeAsync.
@Override
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, byte[] bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
try {
final Response response = execute(userAgent, headers, httpVerb, completeUrl, bodyContents);
@SuppressWarnings("unchecked") final T t = converter == null ? (T) response : converter.convert(response);
if (callback != null) {
callback.onCompleted(t);
}
return new JDKHttpFuture<>(t);
} catch (InterruptedException | ExecutionException | IOException e) {
callback.onThrowable(e);
return new JDKHttpFuture<>(e);
}
}
use of com.github.scribejava.core.model.Response in project webapp by elimu-ai.
the class SignOnControllerGoogle method handleCallback.
@RequestMapping(value = "/sign-on/google/callback", method = RequestMethod.GET)
public String handleCallback(HttpServletRequest request, Model model) {
logger.info("handleCallback");
String state = request.getParameter("state");
logger.debug("state: " + state);
if (!secretState.equals(state)) {
return "redirect:/sign-on?error=state_mismatch";
} else {
String code = request.getParameter("code");
logger.debug("code: " + code);
String responseBody = null;
logger.info("Trading the Authorization Code for an Access Token...");
try {
OAuth2AccessToken accessToken = oAuth20Service.getAccessToken(code);
logger.debug("accessToken: " + accessToken);
logger.info("Got the Access Token!");
// Access the protected resource
OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
oAuth20Service.signRequest(accessToken, oAuthRequest);
Response response = oAuth20Service.execute(oAuthRequest);
responseBody = response.getBody();
logger.info("response.getCode(): " + response.getCode());
logger.info("response.getBody(): " + response.getBody());
} catch (IOException | InterruptedException | ExecutionException ex) {
logger.error(ex);
return "redirect:/sign-on?error=" + ex.getMessage();
}
JSONObject jsonObject = new JSONObject(responseBody);
logger.info("jsonObject: " + jsonObject);
Contributor contributor = new Contributor();
contributor.setEmail(jsonObject.getString("email"));
contributor.setProviderIdGoogle(jsonObject.getString("sub"));
contributor.setImageUrl(jsonObject.getString("picture"));
if (jsonObject.has("given_name")) {
contributor.setFirstName(jsonObject.getString("given_name"));
}
if (jsonObject.has("family_name")) {
contributor.setLastName(jsonObject.getString("family_name"));
}
// Look for existing Contributor with matching e-mail address
Contributor existingContributor = contributorDao.read(contributor.getEmail());
logger.info("existingContributor: " + existingContributor);
if (existingContributor == null) {
// Store new Contributor in database
contributor.setRegistrationTime(Calendar.getInstance());
contributor.setRoles(new HashSet<>(Arrays.asList(Role.CONTRIBUTOR)));
contributorDao.create(contributor);
logger.info("Contributor " + contributor.getEmail() + " was created at " + request.getServerName());
} else {
// Update existing contributor with latest values fetched from provider
if (StringUtils.isNotBlank(contributor.getProviderIdGoogle())) {
existingContributor.setProviderIdGoogle(contributor.getProviderIdGoogle());
}
if (StringUtils.isNotBlank(contributor.getImageUrl())) {
existingContributor.setImageUrl(contributor.getImageUrl());
}
if (StringUtils.isNotBlank(contributor.getFirstName())) {
existingContributor.setFirstName(contributor.getFirstName());
}
if (StringUtils.isNotBlank(contributor.getLastName())) {
existingContributor.setLastName(contributor.getLastName());
}
contributorDao.update(existingContributor);
contributor = existingContributor;
}
// Authenticate
new CustomAuthenticationManager().authenticateUser(contributor);
// Add Contributor object to session
request.getSession().setAttribute("contributor", contributor);
return "redirect:/content";
}
}
Aggregations