/** * 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); } }
.setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(id) .setClientSecret(secret)
/** * 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); } }
.setGrantType(GrantType.REFRESH_TOKEN) .setRefreshToken(rToken.getRefreshToken()) .buildBodyMessage();
OAuthClientRequest request = OAuthClientRequest.tokenLocation(hostName+"/oauth2/token"). setClientId(clientId).setClientSecret(clientSecret). setGrantType(GrantType.PASSWORD). setRedirectURI(""). setUsername(username).
.setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(redditApp.getClientID()) .setClientSecret(redditApp.getClientSecret())
OAuthClientRequest oauthRequest = OAuthClientRequest .tokenProvider(providerType) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(clientId) .setClientSecret(clientSecret)
public OAuthAccessTokenResponse obtainAuth2Token(OAuth2Provider provider, String accessCode) throws OAuthSystemException, OAuthProblemException { log.trace("obtainAuth2Token code={}, provider={}", accessCode, provider); String oAuth2ClientId = provider.getClientId(); String oAuth2TokenLocation = provider.getTokenLocation(); String oAuth2ClientSecret = provider.getClientSecret(); String oAuth2RedirectURI = provider.getRedirectURI(); OAuthClientRequest oAuthRequest = OAuthClientRequest .tokenLocation(oAuth2TokenLocation) .setGrantType(GrantType.AUTHORIZATION_CODE) .setRedirectURI(oAuth2RedirectURI) .setCode(accessCode) .setClientId(oAuth2ClientId) .setClientSecret(oAuth2ClientSecret) .buildBodyMessage(); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); // This works for facebook OAuthAccessTokenResponse oAuth2Response2 = oAuthClient.accessToken(oAuthRequest, OAuth2TokenResponse.class); //return oAuth2Response; // This might work for google OAuthJSONAccessTokenResponse o; //OAuthAccessTokenResponse oAuth2Response2 = oAuthClient.accessToken(oAuthRequest, OAuth2TokenResponse.class); return oAuth2Response2; }
/** * 根据授权码获取accessToken * * @param authCode * @return * @throws OAuthProblemException * @throws OAuthSystemException */ private static OAuthAccessTokenResponse makeTokenRequestWithAuthCode(String authCode) throws OAuthProblemException, OAuthSystemException { OAuthClientRequest request = OAuthClientRequest .tokenLocation(ClientParams.OAUTH_SERVER_TOKEN_URL) .setClientId(ClientParams.CLIENT_ID) .setClientSecret(ClientParams.CLIENT_SECRET) .setGrantType(GrantType.AUTHORIZATION_CODE) .setCode(authCode) .setRedirectURI(ClientParams.OAUTH_SERVER_REDIRECT_URI) .buildBodyMessage(); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthAccessTokenResponse oauthResponse = oAuthClient.accessToken(request); System.out.println("Access Token: " + oauthResponse.getAccessToken()); System.out.println("Expires In: " + oauthResponse.getExpiresIn()); getAuthedService(oauthResponse.getAccessToken()); return oauthResponse; }
public OAuth(Client client, OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(client, OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes), OAuthClientRequest.authorizationLocation(authorizationUrl).setScope(scopes)); switch (flow) { case accessCode: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); authenticationRequestBuilder.setResponseType(OAuth.CODE); break; case implicit: tokenRequestBuilder.setGrantType(GrantType.IMPLICIT); authenticationRequestBuilder.setResponseType(OAuth.TOKEN); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
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); } }
@Override public Optional<AccessTokenResponse> getAccessToken(final HttpServletRequest req, final String redirectUri) { try { OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(req); OAuthClientRequest request = OAuthClientRequest .tokenLocation(tokenEndpointURL) .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectUri) .setGrantType(GrantType.AUTHORIZATION_CODE) .setCode(oar.getCode()) .buildBodyMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); return Optional.of(client.accessToken(request, OltuAccessTokenResponse.class)); } catch (OAuthSystemException | OAuthProblemException e) { LOGGER.error("Authentication failed.", e); return Optional.empty(); } }
private OAuthClientRequest getAccessRequest(String tokenEndPoint, String clientId, String code, String clientSecret, String callbackurl) throws AuthenticationFailedException { OAuthClientRequest accessRequest = null; try { accessRequest = OAuthClientRequest.tokenLocation(tokenEndPoint) .setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(clientId) .setClientSecret(clientSecret).setRedirectURI(callbackurl).setCode(code) .buildBodyMessage(); } catch (OAuthSystemException e) { if (log.isDebugEnabled()) { log.debug("Exception while building request for request access token", e); } throw new AuthenticationFailedException(e.getMessage(), e); } return accessRequest; }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }
public void setFlow(OAuthFlow flow) { switch(flow) { case accessCode: case implicit: tokenRequestBuilder.setGrantType(GrantType.AUTHORIZATION_CODE); break; case password: tokenRequestBuilder.setGrantType(GrantType.PASSWORD); break; case application: tokenRequestBuilder.setGrantType(GrantType.CLIENT_CREDENTIALS); break; default: break; } }