/** Authorizes the installed application to access user's protected data. */ private static Credential authorize() throws Exception { // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(AdSenseSample.class.getResourceAsStream("/client_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out.println("Enter Client ID and Secret from " + "https://code.google.com/apis/console/?api=adsense into " + "adsense-cmdline-sample/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, Collections.singleton(AdSenseScopes.ADSENSE_READONLY)).setDataStoreFactory( dataStoreFactory).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }
/** Authorizes the installed application to access user's protected data. * @throws Exception */ private static Credential authorize() throws Exception { // load client secrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(Main.class.getResourceAsStream("/client_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter")) { System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ " + "into google-sites-liberation/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory( dataStoreFactory).setAccessType("offline").build(); //Loading receiver on 8080 port (you can change this if already in use) LocalServerReceiver localServerReceiver = new LocalServerReceiver.Builder().setPort( 8080 ).build(); // authorize return new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user"); }
public static boolean initilize() { try { dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader("client_secrets.json")); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { logger.error("client_secrets.json not found"); return false; } GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, Collections.singleton(GmailScopes.GMAIL_COMPOSE)).setDataStoreFactory( dataStoreFactory).build(); credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); return true; } catch (IOException | GeneralSecurityException ex) { logger.error("Error initializing GmailClient", ex); } return false; }
/** * Ensure the client secrets file has been filled out. * * @param clientSecrets the GoogleClientSecrets containing data from the * file */ private static void checkClientSecretsFile( GoogleClientSecrets clientSecrets ) { if ( clientSecrets.getDetails().getClientId().startsWith( "[[INSERT" ) || clientSecrets.getDetails().getClientSecret().startsWith( "[[INSERT" ) ) { LOG.error( "Enter Client ID and Secret from " + "APIs console into resources/client_secrets.json." ); System.exit( 1 ); } }
public static Credential authorize() throws IOException { Credential credential; try { GoogleClientSecrets clientSecrets = new GoogleClientSecrets(); GoogleClientSecrets.Details det = new GoogleClientSecrets.Details(); det.setClientId("my id"); det.setClientSecret("my secret"); det.setRedirectUris(Arrays.asList("urn:ietf:wg:oauth:2.0:oob")); clientSecrets.setInstalled(det); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory(DATA_STORE_FACTORY) .setAccessType("offline") .build(); credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); } catch (Exception ex) { credential = null; } return credential; }
@Override public GoogleClientSecrets build() throws ValidationException { validate(); Details details = new Details(); details.setClientId(clientId); details.setClientSecret(clientSecret); GoogleClientSecrets googleClientSecrets = new GoogleClientSecrets(); googleClientSecrets.setInstalled(details); return googleClientSecrets; }
private String authorize(String redirectUri) { AuthorizationCodeRequestUrl authorizationUrl; Details web = new Details(); GoogleCalendarSettings googleCalendarSettings = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings(); web.setClientId(googleCalendarSettings.getClientId()); web.setClientSecret(googleCalendarSettings.getClientSecret()); GoogleClientSecrets clientSecrets = new GoogleClientSecrets(); clientSecrets.setWeb(web); flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)) .setApprovalPrompt("force") .setAccessType("offline") .build(); authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(redirectUri); LOG.info("using authorizationUrl " + authorizationUrl); return authorizationUrl.build(); } }
/** * Tests that the builder correctly reads from a file. */ @Test public void testGoogleSecretsReadPropertiesFromFile() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); when(configurationHelper.fromFile("path")).thenReturn(config); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); GoogleClientSecrets googleClientSecrets = builder.fromFile("path").build(); assertEquals("clientId", googleClientSecrets.getInstalled().getClientId()); assertEquals("clientSecret", googleClientSecrets.getInstalled().getClientSecret()); }
/** Authorizes the installed application to access user's protected data. * @throws Exception */ private static Credential authorize() throws Exception { // load client secrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(Main.class.getResourceAsStream("/client_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter")) { System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ " + "into google-sites-liberation/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory( dataStoreFactory).setAccessType("offline").build(); //Loading receiver on 8080 port (you can change this if already in use) LocalServerReceiver localServerReceiver = new LocalServerReceiver.Builder().setPort( 8080 ).build(); // authorize return new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user"); }
@Override public Credential build() throws IOException { GoogleClientSecrets secrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(clientSecretFile)); // make a sanity check (check this.installed) before authorizing secrets.getDetails().getClientId(); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, secrets, scopes).setDataStoreFactory(dataStoreFactory).setAccessType("offline").build(); return new AuthorizationCodeInstalledAppTalend(flow, new LocalServerReceiver()).authorize("user"); } }
private static void tokenInfo(String accessToken) throws IOException { header("Validating a token"); Tokeninfo tokeninfo = oauth2.tokeninfo().setAccessToken(accessToken).execute(); System.out.println(tokeninfo.toPrettyString()); if (!tokeninfo.getAudience().equals(clientSecrets.getDetails().getClientId())) { System.err.println("ERROR: audience does not match our client ID!"); } }
@Override public GoogleClientSecrets build() throws ValidationException { validate(); Details details = new Details(); details.setClientId(clientId); details.setClientSecret(clientSecret); GoogleClientSecrets googleClientSecrets = new GoogleClientSecrets(); googleClientSecrets.setInstalled(details); return googleClientSecrets; }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testGoogleSecretsReadPropertiesFromConfiguration() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); GoogleClientSecrets googleClientSecrets = new GoogleClientSecretsBuilder() .forApi(GoogleClientSecretsBuilder.Api.AD_MANAGER) .from(config) .build(); assertEquals("clientId", googleClientSecrets.getInstalled().getClientId()); assertEquals("clientSecret", googleClientSecrets.getInstalled().getClientSecret()); }
/** Authorizes the installed application to access user's protected data. */ private static Credential authorize() throws Exception { // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(StreetViewPublishSample.class.getResourceAsStream("/client_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out.println( "Enter Client ID and Secret from https://console.cloud.google.com/apis/credentials " + "into streetview-publish-cmdline-sample/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, Arrays.asList("https://www.googleapis.com/auth/streetviewpublish") ).setDataStoreFactory( dataStoreFactory).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }
private static Credentials getUserCredentials(String credentialsPath, List<String> selectedScopes) throws IOException, GeneralSecurityException { GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsPath))); String clientId = clientSecrets.getDetails().getClientId(); String clientSecret = clientSecrets.getDetails().getClientSecret(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, selectedScopes) .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) .setAccessType("offline") .build(); LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build(); Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); return UserCredentials.newBuilder() .setClientId(clientId) .setClientSecret(clientSecret) .setRefreshToken(credential.getRefreshToken()) .build(); } }
@Override public Credential build() throws IOException { GoogleClientSecrets secrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(clientSecretFile)); // make a sanity check (check this.installed) before authorizing secrets.getDetails().getClientId(); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, secrets, scopes).setDataStoreFactory(dataStoreFactory).setAccessType("offline").build(); return new AuthorizationCodeInstalledAppTalend(flow, new LocalServerReceiver()).authorize("user"); } }
/** * @param clientSecrets OAuth 2.0 client secrets JSON model as specified in <a * href="https://developers.google.com/api-client-library/python/guide/aaa_client_secrets"> * client_secrets.json file format</a> * @param redirectUri URI that the authorization server directs the resource owner's user-agent * back to the client after a successful authorization grant * @param scopes scopes (see {@link #setScopes(Collection)}) * * @since 1.15 */ public GoogleBrowserClientRequestUrl( GoogleClientSecrets clientSecrets, String redirectUri, Collection<String> scopes) { this(clientSecrets.getDetails().getClientId(), redirectUri, scopes); }
new GoogleClientSecrets.Details().setClientId(clientId).setClientSecret(clientSecret); GoogleClientSecrets clientSecrets = new GoogleClientSecrets().setInstalled(details); new GoogleAuthorizationCodeFlow.Builder(transport, JSON_FACTORY, clientSecrets, scopes) .setCredentialStore(credentialStore) .setRequestInitializer(new CredentialHttpRetryInitializer()) .setTokenServerUrl(new GenericUrl(TOKEN_SERVER_URL)) .build();
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testGoogleSecretsReadPropertiesFromConfiguration_properPrefix() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientIdDfp"); config.setProperty("api.admanager.clientSecret", "clientSecretDfp"); config.setProperty("api.adwords.clientId", "clientIdAdWords"); config.setProperty("api.adwords.clientSecret", "clientSecretAdWords"); GoogleClientSecrets googleClientSecrets = new GoogleClientSecretsBuilder() .forApi(GoogleClientSecretsBuilder.Api.AD_MANAGER) .from(config) .build(); assertEquals("clientIdDfp", googleClientSecrets.getInstalled().getClientId()); assertEquals("clientSecretDfp", googleClientSecrets.getInstalled().getClientSecret()); }
/** Authorizes the installed application to access user's protected data. */ private static Credential authorize() throws Exception { // load client secrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(OAuth2Sample.class.getResourceAsStream("/client_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ " + "into oauth2-cmdline-sample/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory( dataStoreFactory).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }