/** * Creates a {@link Credential} objects with the given {@link TokensAndUrlAuthData} which supports * refreshing tokens. */ public Credential createCredential(TokensAndUrlAuthData authData) { return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setClientAuthentication( new ClientParametersAuthentication(appCredentials.getKey(), appCredentials.getSecret())) .setTokenServerEncodedUrl(authData.getTokenServerEncodedUrl()) .build() .setAccessToken(authData.getAccessToken()) .setRefreshToken(authData.getRefreshToken()) .setExpiresInSeconds(EXPIRE_TIME_IN_SECONDS); } }
TokenResponse tokenResponse = new TokenResponse(); tokenResponse.setAccessToken(accessToken); tokenResponse.setRefreshToken(refreshToken); tokenResponse.setExpiresInSeconds(3600L); tokenResponse.setScope("https://www.googleapis.com/auth/androidpublisher"); tokenResponse.setTokenType("Bearer");
private String generateParamsString(Optional<Map<String, String>> params) { Map<String, String> updatedParams = new ArrayMap<>(); if (params.isPresent()) { updatedParams.putAll(params.get()); } if (!updatedParams.containsKey(ACCESS_TOKEN_KEY)) { updatedParams.put(ACCESS_TOKEN_KEY, credential.getAccessToken()); } List<String> orderedKeys = updatedParams.keySet().stream().collect(Collectors.toList()); Collections.sort(orderedKeys); List<String> paramStrings = new ArrayList<>(); for (String key : orderedKeys) { String k = key.trim(); String v = updatedParams.get(key).trim(); paramStrings.add(k + "=" + v); } return String.join("&", paramStrings); }
protected void onAuthorization( AuthorizationCodeRequestUrl authorizationUrl ) throws IOException { String url = authorizationUrl.build(); Spoon spoon = Spoon.getInstance(); if ( spoon != null ) { Display.getDefault().syncExec( new Runnable() { public void run() { Shell shell = spoon.getShell(); GoogleAuthorizationDialog authorizationDialog = new GoogleAuthorizationDialog( shell, getReceiver() ); authorizationDialog.open( url ); } } ); } else { browse( url ); } } }
public GoogleWebmasterClientImpl(Credential credential, String appName) throws IOException { //transport: new NetHttpTransport() or GoogleNetHttpTransport.newTrustedTransport() //jsonFactory: new JacksonFactory() or JacksonFactory.getDefaultInstance() _service = new Webmasters.Builder(credential.getTransport(), GoogleCommon.getJsonFactory(), credential) .setApplicationName(appName).build(); _analytics = _service.searchanalytics(); }
} catch ( TokenResponseException tre ) { Exception exceptionToLog = tre; if ( tre.getDetails() != null && tre.getDetails().getError() != null ) { exceptionToLog = new IOException( BaseMessages.getString( PKG, "GoogleAnalytics.Error.OAuth2.Auth", tre.getDetails().getError() ), tre );
public boolean handleResponse( final HttpRequest request, final HttpResponse response, final boolean supportsRetry) throws IOException { if (wrappedCredential.handleResponse(request, response, supportsRetry)) { // If credential decides it can handle it, the return code or message indicated // something specific to authentication, and no backoff is desired. return true; } else if (backoffHandler.handleResponse(request, response, supportsRetry)) { // Otherwise, we defer to the judgement of our internal backoff handler. LOG.info("Retrying " + request.getUrl().toString()); return true; } else { return false; } } });
@Override public GoogleRefreshTokenRequest setClientAuthentication( HttpExecuteInterceptor clientAuthentication) { return (GoogleRefreshTokenRequest) super.setClientAuthentication(clientAuthentication); }
@Override public GoogleAuthorizationCodeTokenRequest setRequestInitializer( HttpRequestInitializer requestInitializer) { return (GoogleAuthorizationCodeTokenRequest) super.setRequestInitializer(requestInitializer); }
@Override public GoogleBrowserClientRequestUrl clone() { return (GoogleBrowserClientRequestUrl) super.clone(); } }
/** * @since 1.11 */ @Override public Builder setTransport(HttpTransport transport) { return (Builder) super.setTransport(transport); }
/** * @since 1.11 */ @Override public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) { return (Builder) super.setClientAuthentication(clientAuthentication); }
/** * @since 1.11 */ @Override public Builder setAuthorizationServerEncodedUrl(String authorizationServerEncodedUrl) { return (Builder) super.setAuthorizationServerEncodedUrl(authorizationServerEncodedUrl); }
@Override public GoogleTokenResponse clone() { return (GoogleTokenResponse) super.clone(); } }
/** * @since 1.11 */ @Override public Builder setMethod(AccessMethod method) { return (Builder) super.setMethod(method); }
public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException { Map<String, List<String>> map = new HashMap<String, List<String>>(); List<String> list = new ArrayList<String>(); list.add("Bearer " + credential.getAccessToken()); map.put("Authorization", list); return map; } public boolean hasRequestMetadata() { return true; }
@Override public synchronized void initialize(URI uri, Configuration conf) throws IOException { if (this.client == null) { super.initialize(uri, conf); State state = HadoopUtils.getStateFromConf(conf); Credential credential = new GoogleCommon.CredentialBuilder(state.getProp(SOURCE_CONN_PRIVATE_KEY), state.getPropAsList(API_SCOPES)) .fileSystemUri(state.getProp(PRIVATE_KEY_FILESYSTEM_URI)) .proxyUrl(state.getProp(SOURCE_CONN_USE_PROXY_URL)) .port(state.getProp(SOURCE_CONN_USE_PROXY_PORT)) .serviceAccountId(state.getProp(SOURCE_CONN_USERNAME)) .build(); this.client = new Drive.Builder(credential.getTransport(), GoogleCommon.getJsonFactory(), credential) .setApplicationName(Preconditions.checkNotNull(state.getProp(APPLICATION_NAME), "ApplicationName is required")) .build(); this.pageSize = state.getPropAsInt(PAGE_SIZE, DEFAULT_PAGE_SIZE); } }
public static void main(String[] args) { try { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // authorization Credential credential = authorize(); // set up global Oauth2 instance oauth2 = new Oauth2.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName( APPLICATION_NAME).build(); // run commands tokenInfo(credential.getAccessToken()); userInfo(); // success! return; } catch (IOException e) { System.err.println(e.getMessage()); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); }
/** * Initialize file system helper at most once for this instance. * {@inheritDoc} * @see org.apache.gobblin.source.extractor.filebased.FileBasedSource#initFileSystemHelper(org.apache.gobblin.configuration.State) */ @Override public synchronized void initFileSystemHelper(State state) throws FileBasedHelperException { if (fsHelper == null) { Credential credential = new GoogleCommon.CredentialBuilder(state.getProp(SOURCE_CONN_PRIVATE_KEY), state.getPropAsList(API_SCOPES)) .fileSystemUri(state.getProp(PRIVATE_KEY_FILESYSTEM_URI)) .proxyUrl(state.getProp(SOURCE_CONN_USE_PROXY_URL)) .port(state.getProp(SOURCE_CONN_USE_PROXY_PORT)) .serviceAccountId(state.getProp(SOURCE_CONN_USERNAME)) .build(); Drive driveClient = new Drive.Builder(credential.getTransport(), GoogleCommon.getJsonFactory(), credential) .setApplicationName(Preconditions.checkNotNull(state.getProp(APPLICATION_NAME), "ApplicationName is required")) .build(); this.fsHelper = closer.register(new GoogleDriveFsHelper(state, driveClient)); } }
.build(); this.gaService = new Analytics.Builder(credential.getTransport(), GoogleCommon.getJsonFactory(), credential) .build(); Drive driveClient = new Drive.Builder(credential.getTransport(), GoogleCommon.getJsonFactory(), Preconditions.checkNotNull(credential, "Credential is required"))