Search in sources :

Example 1 with OAuthException

use of org.scribe.exceptions.OAuthException in project webapp by elimu-ai.

the class Google2Api method getAccessTokenExtractor.

@Override
public AccessTokenExtractor getAccessTokenExtractor() {
    return new AccessTokenExtractor() {

        @Override
        public Token extract(String response) {
            Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
            Matcher matcher = Pattern.compile("\"access_token\" : \"([^&\"]+)\"").matcher(response);
            if (matcher.find()) {
                String token = OAuthEncoder.decode(matcher.group(1));
                return new Token(token, "", response);
            } else {
                throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
            }
        }
    };
}
Also used : Matcher(java.util.regex.Matcher) OAuthException(org.scribe.exceptions.OAuthException) Token(org.scribe.model.Token) AccessTokenExtractor(org.scribe.extractors.AccessTokenExtractor)

Example 2 with OAuthException

use of org.scribe.exceptions.OAuthException in project OpenOLAT by OpenOLAT.

the class Google2Api method getAccessTokenExtractor.

@Override
public AccessTokenExtractor getAccessTokenExtractor() {
    return new AccessTokenExtractor() {

        @Override
        public Token extract(String response) {
            Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
            Matcher matcher = Pattern.compile("\"access_token\" : \"([^&\"]+)\"").matcher(response);
            if (matcher.find()) {
                String token = OAuthEncoder.decode(matcher.group(1));
                return new Token(token, "", response);
            } else {
                throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
            }
        }
    };
}
Also used : Matcher(java.util.regex.Matcher) OAuthException(org.scribe.exceptions.OAuthException) Token(org.scribe.model.Token) AccessTokenExtractor(org.scribe.extractors.AccessTokenExtractor)

Example 3 with OAuthException

use of org.scribe.exceptions.OAuthException in project muikku by otavanopisto.

the class GoogleApi20ServiceImpl method getAccessToken.

@Override
public Token getAccessToken(Token requestToken, Verifier verifier) {
    OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
    request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
    request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
    request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
    request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
    request.addBodyParameter("grant_type", "authorization_code");
    if (config.hasScope())
        request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
    Response response = request.send();
    ObjectMapper objectMapper = new ObjectMapper();
    String tokenJson;
    try {
        tokenJson = objectMapper.writeValueAsString(objectMapper.readTree(response.getBody()));
    } catch (IOException e) {
        throw new OAuthException("Invalid Token JSON", e);
    }
    return api.getAccessTokenExtractor().extract(tokenJson);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) OAuthException(org.scribe.exceptions.OAuthException) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 4 with OAuthException

use of org.scribe.exceptions.OAuthException in project camel by apache.

the class JsonTokenExtractor method extract.

public Token extract(String response) {
    Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
    Matcher matcher = accessTokenPattern.matcher(response);
    if (matcher.find()) {
        return new Token(matcher.group(1), "", response);
    } else {
        throw new OAuthException("Cannot extract an acces token. Response was: " + response);
    }
}
Also used : Matcher(java.util.regex.Matcher) OAuthException(org.scribe.exceptions.OAuthException) Token(org.scribe.model.Token)

Example 5 with OAuthException

use of org.scribe.exceptions.OAuthException in project muikku by otavanopisto.

the class PyramusApi20ServiceImpl method getAccessToken.

@Override
public Token getAccessToken(Token requestToken, Verifier verifier) {
    OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
    request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
    request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
    request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
    request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
    request.addBodyParameter("grant_type", "authorization_code");
    if (config.hasScope())
        request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
    Response response = request.send();
    ObjectMapper objectMapper = new ObjectMapper();
    String tokenJson;
    try {
        tokenJson = objectMapper.writeValueAsString(objectMapper.readTree(response.getBody()));
    } catch (IOException e) {
        throw new OAuthException("Invalid Token JSON", e);
    }
    return api.getAccessTokenExtractor().extract(tokenJson);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) OAuthException(org.scribe.exceptions.OAuthException) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

OAuthException (org.scribe.exceptions.OAuthException)6 Matcher (java.util.regex.Matcher)4 Token (org.scribe.model.Token)4 AccessTokenExtractor (org.scribe.extractors.AccessTokenExtractor)3 IOException (java.io.IOException)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 OAuthRequest (org.scribe.model.OAuthRequest)2 Response (org.scribe.model.Response)2