Java Code Examples for com.github.scribejava.core.model.Response#getBody()

The following examples show how to use com.github.scribejava.core.model.Response#getBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JamAuthConfig.java    From jam-collaboration-sample with Apache License 2.0 6 votes vote down vote up
public String getSingleUseToken() {
    OAuth10aService service = JamAuthConfig.instance().getOAuth10aService();
    final OAuthRequest request = new OAuthRequest(Verb.POST,
            JamAuthConfig.instance().getServerUrl() + "/v1/single_use_tokens",
            service);
    service.signRequest(JamAuthConfig.instance().getOAuth10aAccessToken(), request);

    final Response response = request.send();
    String body = response.getBody();

    Matcher matcher = SINGLE_USE_TOKEN_PATTERN.matcher(body);
    if (matcher.find()) {
        return matcher.group(0);
    }
    return null;
}
 
Example 2
Source File: GoogleController.java    From tutorials with MIT License 6 votes vote down vote up
@GetMapping(value = "/auth/google")
public String google(@RequestParam String code, HttpServletResponse servletResponse){

    try {
        OAuth2AccessToken token = service.getService().getAccessToken(code);

        OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
        service.getService().signRequest(token, request);
        Response response = service.getService().execute(request);
        return response.getBody();

    }catch (Exception e){
        servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }

    return null;
}
 
Example 3
Source File: Network.java    From mirror with MIT License 4 votes vote down vote up
/**
 * Like {@link #get(String)}, but for OAuth authenticated requests.
 */
public static String get(Activity activity, String urlString, DefaultApi20 api,
                         OAuthDataProvider data) {
  if (urlString == null) {
    return null;
  }
  Log.d(TAG, "Requesting OAuth URL: " + urlString);

  try {
    OAuth20Service service = new ServiceBuilder(data.getClientId())
        .apiSecret(data.getClientSecret())
        .build(api);

    // Look for any saved access token. If there is none, refresh using the initial refresh token.
    // If there is one but it is expired, refresh using the saved refresh token.
    AccessToken accessToken = loadAccessToken(activity, data);
    if ((accessToken == null) || accessToken.shouldRefreshNow()) {
      Log.w(TAG, "Refreshing access token.");

      // Figure out which refresh token to use.
      String refreshToken;
      if (accessToken == null) {
        Log.d(TAG, "Using initial refresh token.");
        refreshToken = data.getRefreshToken();
      } else {
        Log.d(TAG, "Using saved refresh token.");
        refreshToken = accessToken.getRefreshToken();
      }

      // Get the new access token.
      long refreshTime = System.currentTimeMillis() / 1000;
      accessToken = new AccessToken(service.refreshAccessToken(refreshToken), refreshTime);

      // Save it for next time.
      saveAccessToken(activity, data, accessToken, refreshTime);
    }

    // Make the authenticated request.
    OAuthRequest request = new OAuthRequest(Verb.GET, urlString);
    service.signRequest(accessToken, request);
    Response response = service.execute(request);

    return response.getBody();
  } catch (IOException | InterruptedException | ExecutionException e) {
    Log.e(TAG, "OAuth request failed.", e);
    return null;
  }
}
 
Example 4
Source File: UserController.java    From tutorials with MIT License 4 votes vote down vote up
@GetMapping("/me/myapi")
public String me(@RequestParam String username, @RequestParam String password, HttpServletResponse responsehttp) {

    try {
        OAuth2AccessToken token = service.getService().getAccessTokenPasswordGrant(username, password);

        OAuthRequest request = new OAuthRequest(Verb.GET, "http://localhost:8080/me");
        service.getService().signRequest(token, request);
        Response response = service.getService().execute(request);

        return response.getBody();

    } catch (Exception e) {
        responsehttp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }

    return null;

}