/** * Retrieve access token, if needed, retrieve instance url, and set server host URL * {@inheritDoc} * @see org.apache.gobblin.writer.http.HttpWriter#onConnect(org.apache.http.HttpHost) */ @Override public void onConnect(URI serverHost) throws IOException { if (!StringUtils.isEmpty(accessToken)) { return; //No need to be called if accessToken is active. } try { getLog().info("Getting Oauth2 access token."); OAuthClientRequest request = OAuthClientRequest.tokenLocation(serverHost.toString()) .setGrantType(GrantType.PASSWORD) .setClientId(clientId) .setClientSecret(clientSecret) .setUsername(userId) .setPassword(password + securityToken).buildQueryMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); OAuthJSONAccessTokenResponse response = client.accessToken(request, OAuth.HttpMethod.POST); accessToken = response.getAccessToken(); setCurServerHost(new URI(response.getParam("instance_url"))); } catch (OAuthProblemException e) { throw new NonTransientException("Error while authenticating with Oauth2", e); } catch (OAuthSystemException e) { throw new RuntimeException("Failed getting access token", e); } catch (URISyntaxException e) { throw new RuntimeException("Failed due to invalid instance url", e); } }
@Override public String getAccessToken() { return getParam(OAuth.OAUTH_ACCESS_TOKEN); }
@Override public String getTokenType() { return getParam(OAuth.OAUTH_TOKEN_TYPE); }
@Override public String getTokenType() { return getParam(OAuth.OAUTH_TOKEN_TYPE); }
@Override public String getAccessToken() { return getParam(OAuth.OAUTH_ACCESS_TOKEN); }
public String getScope() { return getParam(OAuth.OAUTH_SCOPE); }
public String getRefreshToken() { return getParam(OAuth.OAUTH_REFRESH_TOKEN); }
public String getScope() { return getParam(OAuth.OAUTH_SCOPE); }
public String getRefreshToken() { return getParam(OAuth.OAUTH_REFRESH_TOKEN); }
@Override public Long getExpiresIn() { String value = getParam(OAuth.OAUTH_EXPIRES_IN); return value == null? null: Long.valueOf(value); }
@Override public Long getExpiresIn() { String value = getParam(OAuth.OAUTH_EXPIRES_IN); return value == null? null: Long.valueOf(value); }
/** * Refresh this reddit token with data received from the new token. * * @param token Token received from a refresh request to reddit */ public void refresh(OAuthJSONAccessTokenResponse token) { this.accessToken = token.getAccessToken(); this.expiration = currentTimeSeconds() + token.getExpiresIn(); this.expirationSpan = token.getExpiresIn(); this.scopes = new RedditTokenCompleteScope(token.getScope()); this.tokenType = token.getParam(PARAM_TOKEN_TYPE); }
/** * @param token JSON response after an OAuth2 token request */ protected RedditToken(OAuthJSONAccessTokenResponse token) { this.accessToken = token.getAccessToken(); this.refreshToken = token.getRefreshToken(); this.expiration = currentTimeSeconds() + token.getExpiresIn(); this.expirationSpan = token.getExpiresIn(); this.scopes = new RedditTokenCompleteScope(token.getScope()); this.tokenType = token.getParam(PARAM_TOKEN_TYPE); }
/** * Retrieve access token, if needed, retrieve instance url, and set server host URL * {@inheritDoc} * @see org.apache.gobblin.writer.http.HttpWriter#onConnect(org.apache.http.HttpHost) */ @Override public void onConnect(URI serverHost) throws IOException { if (!StringUtils.isEmpty(accessToken)) { return; //No need to be called if accessToken is active. } try { getLog().info("Getting Oauth2 access token."); OAuthClientRequest request = OAuthClientRequest.tokenLocation(serverHost.toString()) .setGrantType(GrantType.PASSWORD) .setClientId(clientId) .setClientSecret(clientSecret) .setUsername(userId) .setPassword(password + securityToken).buildQueryMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); OAuthJSONAccessTokenResponse response = client.accessToken(request, OAuth.HttpMethod.POST); accessToken = response.getAccessToken(); setCurServerHost(new URI(response.getParam("instance_url"))); } catch (OAuthProblemException e) { throw new NonTransientException("Error while authenticating with Oauth2", e); } catch (OAuthSystemException e) { throw new RuntimeException("Failed getting access token", e); } catch (URISyntaxException e) { throw new RuntimeException("Failed due to invalid instance url", e); } }