/** * 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); } }
/** * 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); } }
public <T extends OAuthAccessTokenResponse> T getToken(Class<T> tokenResponseClass) { try { TokenRequestBuilder builder = OAuthClientRequest// .tokenLocation(tokenLocation.toString())// .setGrantType(grantType)// .setClientId(clientID)// .setClientSecret(clientSecret); if (GrantType.AUTHORIZATION_CODE == grantType) { builder = builder.setRedirectURI(callbackURL.toString())// .setCode(getAuthorizationCode()); } else if (GrantType.REFRESH_TOKEN == grantType) { builder = builder.setRefreshToken(refreshToken); } OAuthClientRequest request = builder.buildQueryMessage(); OAuthClient oauthClient = new OAuthClient(new URLConnectionClient()); return oauthClient.accessToken(request, tokenResponseClass); } catch (OAuthSystemException e) { throw new RuntimeException(e); } catch (OAuthProblemException e) { throw new RuntimeException(e); } }
private OAuthClientRequest buidTokenRequest( String tokenEndPoint, String clientId, String clientSecret, String callbackurl, String code) throws ApplicationAuthenticatorException { OAuthClientRequest tokenRequest = null; try { tokenRequest = OAuthClientRequest.tokenLocation(tokenEndPoint).setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(callbackurl).setCode(code) .buildQueryMessage(); } catch (OAuthSystemException e) { throw new ApplicationAuthenticatorException("Exception while building access token request.", e); } return tokenRequest; }
private OAuthClientRequest buidTokenRequest( String tokenEndPoint, String clientId, String clientSecret, String callbackurl, String code) throws ApplicationAuthenticatorException { OAuthClientRequest tokenRequest = null; try { tokenRequest = OAuthClientRequest.tokenLocation(tokenEndPoint).setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(callbackurl).setCode(code) .buildQueryMessage(); } catch (OAuthSystemException e) { throw new ApplicationAuthenticatorException("Exception while building access token request.", e); } return tokenRequest; }
protected OAuthClientRequest buidTokenRequest( String tokenEndPoint, String clientId, String clientSecret, String callbackurl, String code) throws ApplicationAuthenticatorException { OAuthClientRequest tokenRequest = null; try { tokenRequest = OAuthClientRequest.tokenLocation(tokenEndPoint).setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(callbackurl).setCode(code) .buildQueryMessage(); } catch (OAuthSystemException e) { throw new ApplicationAuthenticatorException("Exception while building access token request.", e); } return tokenRequest; }