All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.scribejava.apis.polar.PolarJsonTokenExtractor Maven / Gradle / Ivy

There is a newer version: 8.3.3
Show newest version
package com.github.scribejava.apis.polar;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.oauth2.OAuth2Error;
import java.io.IOException;

/**
 * Token related documentation: https://www.polar.com/accesslink-api/#token-endpoint
 */
public class PolarJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {

    protected PolarJsonTokenExtractor() {
    }

    private static class InstanceHolder {

        private static final PolarJsonTokenExtractor INSTANCE = new PolarJsonTokenExtractor();
    }

    public static PolarJsonTokenExtractor instance() {
        return InstanceHolder.INSTANCE;
    }

    @Override
    protected PolarOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
            String refreshToken, String scope, JsonNode response, String rawResponse) {
        return new PolarOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
                response.get("x_user_id").asText(), rawResponse);
    }

    @Override
    public void generateError(Response response) throws IOException {
        final JsonNode errorNode;
        try {
            errorNode = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER.readTree(response.getBody()).get("errors").get(0);
        } catch (JsonProcessingException ex) {
            throw new OAuth2AccessTokenErrorResponse(null, null, null, response);
        }

        OAuth2Error errorCode;
        try {
            errorCode = OAuth2Error
                    .parseFrom(extractRequiredParameter(errorNode, "errorType", response.getBody()).asText());
        } catch (IllegalArgumentException iaE) {
            //non oauth standard error code
            errorCode = null;
        }

        throw new OAuth2AccessTokenErrorResponse(errorCode, errorNode.get("message").asText(), null, response);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy