Source:
public Token addZenodoToken(@ApiParam(hidden = true) @Auth User user, @QueryParam("code") String code) { final AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(ZENODO_URL + "oauth/token"), new ClientParametersAuthentication(zenodoClientID, zenodoClientSecret), zenodoClientID, ZENODO_URL + "oauth/authorize").build(); LOG.info("About to try and grab zenodo access token"); String accessToken; String refreshToken; try { TokenResponse tokenResponse = flow.newTokenRequest(code) .setRequestInitializer(request -> request.getHeaders().setAccept("application/json")).setGrantType("authorization_code") .setRedirectUri(zenodoRedirectUri).execute(); accessToken = tokenResponse.getAccessToken(); refreshToken = tokenResponse.getRefreshToken(); } catch (IOException e) { LOG.error("Retrieving zenodo access token was unsuccessful"); throw new CustomWebApplicationException("Could not retrieve zenodo token based on code", HttpStatus.SC_BAD_REQUEST); } String username = null; if (user != null) { List<Token> tokens = tokenDAO.findZenodoByUserId(user.getId()); if (tokens.isEmpty()) { Token token = new Token(); token.setTokenSource(TokenType.ZENODO_ORG); token.setContent(accessToken); token.setRefreshToken(refreshToken); token.setUserId(user.getId()); if (username != null) { token.setUsername(username); } else { token.setUsername(user.getUsername()); } long create = tokenDAO.create(token); LOG.info("Zenodo token created for {}", user.getUsername()); return tokenDAO.findById(create); } else { LOG.info("Zenodo token already exists for {}", user.getUsername()); throw new CustomWebApplicationException("Zenodo token already exists for " + user.getUsername(), HttpStatus.SC_CONFLICT); } } else { LOG.info("Could not find user"); throw new CustomWebApplicationException("User not found", HttpStatus.SC_CONFLICT); } }

Target:
public Token addZenodoToken(@ApiParam(hidden = true) @Auth User user, @QueryParam("code") String code) { if (code.isEmpty()) { throw new CustomWebApplicationException("Please provide a Zenodo access code", HttpStatus.SC_BAD_REQUEST); } final AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(zenodoUrl + "/oauth/token"), new ClientParametersAuthentication(zenodoClientID, zenodoClientSecret), zenodoClientID, zenodoAuthUrl).build(); LOG.info("About to request zenodo access token"); String accessToken; String refreshToken; try { TokenResponse tokenResponse = flow.newTokenRequest(code) .setRequestInitializer(request -> request.getHeaders().setAccept("application/json")).setGrantType("authorization_code") .setRedirectUri(zenodoRedirectUri).execute(); accessToken = tokenResponse.getAccessToken(); refreshToken = tokenResponse.getRefreshToken(); } catch (IOException e) { LOG.error("Retrieving zenodo access token was unsuccessful.", e); throw new CustomWebApplicationException("Could not retrieve zenodo token based on code " + e.getMessage(), HttpStatus.SC_BAD_REQUEST); } if (user != null) { List<Token> tokens = tokenDAO.findZenodoByUserId(user.getId()); if (tokens.isEmpty()) { Token token = new Token(); token.setTokenSource(TokenType.ZENODO_ORG); token.setContent(accessToken); token.setRefreshToken(refreshToken); token.setUserId(user.getId()); token.setUsername(""); long create = tokenDAO.create(token); LOG.info("Zenodo token created for {}", user.getUsername()); return tokenDAO.findById(create); } else { LOG.info("Zenodo token already exists for {}", user.getUsername()); throw new CustomWebApplicationException("Zenodo token already exists for " + user.getUsername(), HttpStatus.SC_CONFLICT); } } else { LOG.info("Could not find user"); throw new CustomWebApplicationException("User not found", HttpStatus.SC_NOT_FOUND); } }