Search in sources :

Example 1 with OAuthException

use of com.qasymphony.ci.plugin.exception.OAuthException in project jenkin-qtest-plugin by QASymphony.

the class OauthProvider method getAccessToken.

public static String getAccessToken(String url, String apiKey, String secretKey) throws OAuthException {
    StringBuilder sb = new StringBuilder().append(url).append("/oauth/token?grant_type=refresh_token").append("&refresh_token=").append(HttpClientUtils.encode(apiKey));
    Map<String, String> headers = new HashMap<>();
    headers.put(Constants.HEADER_AUTH, secretKey);
    try {
        ResponseEntity entity = HttpClientUtils.post(sb.toString(), headers, null);
        if (HttpStatus.SC_OK != entity.getStatusCode()) {
            throw new OAuthException(entity.getBody(), entity.getStatusCode());
        }
        JsonNode node = JsonUtils.readTree(entity.getBody());
        if (null == node) {
            throw new OAuthException("Cannot get access token from: " + entity.getBody(), entity.getStatusCode());
        }
        return JsonUtils.getText(node, "access_token");
    } catch (Exception e) {
        throw new OAuthException(e.getMessage(), e);
    }
}
Also used : ResponseEntity(com.qasymphony.ci.plugin.utils.ResponseEntity) HashMap(java.util.HashMap) OAuthException(com.qasymphony.ci.plugin.exception.OAuthException) JsonNode(com.fasterxml.jackson.databind.JsonNode) OAuthException(com.qasymphony.ci.plugin.exception.OAuthException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 OAuthException (com.qasymphony.ci.plugin.exception.OAuthException)1 ResponseEntity (com.qasymphony.ci.plugin.utils.ResponseEntity)1 HashMap (java.util.HashMap)1