private String token(OAuthClientRequest request, OAuthClient oAuthClient) throws Exception { String name = provider.getName(); if (name.equalsIgnoreCase("facebook") || name.equalsIgnoreCase("github")) { // application/x-www-form-urlencoded GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class); return oAuthResponse.getAccessToken(); } else { // JSON encoded OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request, OAuthJSONAccessTokenResponse.class); return oAuthResponse.getAccessToken(); } }
/** * 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); } }
OAuthClientRequest request = OAuthClientRequest .tokenLocation(TOKEN_URL) .setClientId(TRUSTED_CLIENT_ID) .setClientSecret(TRUSTED_SECRET) .setGrantType(GrantType.CLIENT_CREDENTIALS) .setScope("all") .buildBodyMessage(); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthJSONAccessTokenResponse tokenResponse = oAuthClient.accessToken( request, OAuthJSONAccessTokenResponse.class); HttpGet httpGet = new HttpGet(CATALOG_URL); httpGet.setHeader("Authorization", "Bearer " + tokenResponse.getAccessToken()); HttpResponse httpResponse = apacheHttpClient.execute(httpGet); assertThat(httpResponse.getStatusLine().getStatusCode()).isEqualTo(200); String responseAsString = EntityUtils.toString(httpResponse.getEntity()); ObjectMapper mapper = new ObjectMapper(); Document document = mapper.readValue(responseAsString, Document.class); assertThat(document.getTitle()).isEqualTo("Identifying and recording user actions to enable automatic online assessment");
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException { if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) { try { OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage()); if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) { setAccessToken(accessTokenResponse.getAccessToken()); if (accessTokenListener != null) { accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken()); } return !getAccessToken().equals(requestAccessToken); } else { return false; } } catch (OAuthSystemException e) { throw new IOException(e); } catch (OAuthProblemException e) { throw new IOException(e); } } return true; }
public OAuthToken getOAuthToken() { return new BasicOAuthToken(getAccessToken(), getTokenType(), getExpiresIn(), getRefreshToken(), getScope()); }
public OAuthToken getOAuthToken() { return new BasicOAuthToken(getAccessToken(), getTokenType(), getExpiresIn(), getRefreshToken(), getScope()); }
/** * 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); } }
if (accessTokenResponse.getAccessToken() == null) { throw new ClientHandlerException("Error while requesting an access token. No 'access_token' found."); setAccessToken(accessTokenResponse.getAccessToken(), accessTokenResponse.getExpiresIn()); if (this.accessTokenListener != null) { this.accessTokenListener.notify((BasicOAuthToken)accessTokenResponse.getOAuthToken());