Search in sources :

Example 86 with Response

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);
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) OAuthConfig(com.github.scribejava.core.model.OAuthConfig)

Example 87 with 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();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 88 with Response

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);
    }
}
Also used : Response(com.github.scribejava.core.model.Response) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 89 with Response

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);
    }
}
Also used : Response(com.github.scribejava.core.model.Response) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 90 with Response

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";
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) JSONObject(org.json.JSONObject) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Contributor(ai.elimu.model.contributor.Contributor) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Response (com.github.scribejava.core.model.Response)96 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)86 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)63 Scanner (java.util.Scanner)60 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)48 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)46 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)21 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)21 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)20 Random (java.util.Random)16 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 HttpUrl (okhttp3.HttpUrl)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 Test (org.junit.Test)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)7 ExecutionException (java.util.concurrent.ExecutionException)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 OAuthConfig (com.github.scribejava.core.model.OAuthConfig)3 NingHttpClientConfig (com.github.scribejava.httpclient.ning.NingHttpClientConfig)3