Search in sources :

Example 1 with TokenResponseDTO

use of org.openhab.binding.ecobee.internal.dto.oauth.TokenResponseDTO in project openhab-addons by openhab.

the class EcobeeAuth method getTokens.

/**
 * Call the Ecobee token endpoint to get the access and refresh tokens. Once successfully retrieved,
 * the access and refresh tokens will be injected into the OHC OAuth service.
 * Warnings are suppressed to avoid the Gson.fromJson warnings.
 */
@SuppressWarnings({ "null", "unused" })
private void getTokens() throws EcobeeAuthException {
    logger.debug("EcobeeAuth: State is {}: Executing step: 'getToken'", state);
    StringBuilder url = new StringBuilder(ECOBEE_TOKEN_URL);
    url.append("?grant_type=ecobeePin");
    url.append("&code=").append(code);
    url.append("&client_id=").append(apiKey);
    logger.trace("EcobeeAuth: Posting token URL={}", url);
    String response = executeUrl("POST", url.toString());
    logger.trace("EcobeeAuth: Got a valid token response: {}", response);
    TokenResponseDTO tokenResponse = EcobeeApi.getGson().fromJson(response, TokenResponseDTO.class);
    if (tokenResponse == null) {
        logger.debug("EcobeeAuth: Got null token response from Ecobee API");
        updateBridgeStatus();
        setState(isPinExpired() ? EcobeeAuthState.NEED_PIN : EcobeeAuthState.NEED_TOKEN);
        return;
    }
    String error = tokenResponse.error;
    if (error != null && !error.isEmpty()) {
        throw new EcobeeAuthException(error + ": " + tokenResponse.errorDescription);
    }
    AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
    accessTokenResponse.setRefreshToken(tokenResponse.refreshToken);
    accessTokenResponse.setAccessToken(tokenResponse.accessToken);
    accessTokenResponse.setScope(tokenResponse.scope);
    accessTokenResponse.setTokenType(tokenResponse.tokenType);
    accessTokenResponse.setExpiresIn(tokenResponse.expiresIn);
    try {
        logger.debug("EcobeeAuth: Importing AccessTokenResponse into oAuthClientService!!!");
        oAuthClientService.importAccessTokenResponse(accessTokenResponse);
        bridgeHandler.updateBridgeStatus(ThingStatus.ONLINE);
        setState(EcobeeAuthState.COMPLETE);
        return;
    } catch (OAuthException e) {
        logger.info("EcobeeAuth: Got OAuthException", e);
    // No other processing needed here
    }
    updateBridgeStatus();
    setState(isPinExpired() ? EcobeeAuthState.NEED_PIN : EcobeeAuthState.NEED_TOKEN);
}
Also used : TokenResponseDTO(org.openhab.binding.ecobee.internal.dto.oauth.TokenResponseDTO) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Aggregations

TokenResponseDTO (org.openhab.binding.ecobee.internal.dto.oauth.TokenResponseDTO)1 AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)1 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)1