/** 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"); }
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; }
private Credential authorize() throws IOException { InputStream in = new FileInputStream( resolveCredentialsPath() + "/" + resourceBundle.getString( "client.secrets" ) ); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, new InputStreamReader( in ) ); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES ) .setDataStoreFactory( DATA_STORE_FACTORY ).setAccessType( "offline" ).build(); Credential credential = new CustomAuthorizationCodeInstalledApp( flow, new CustomLocalServerReceiver() ).authorize( "user" ); return credential; }
/** * Authorizes the installed application to access user's protected data. */ private Credential authorize() throws Exception { // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets .load(jsonFactory, new InputStreamReader(GoogleCalendarSourceImpl.class.getResourceAsStream("/client_secrets.json"))); // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow .Builder(httpTransport, jsonFactory, clientSecrets, Collections.singletonList(READ_WRITE_CALENDAR_SCOPE)) .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 class AuthorizationHelper { public static Credential authorize() throws Exception { java.io.File DATA_STORE_DIR = new java.io.File("permissionsFile", "PermissionsFolder"); JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(new FileInputStream("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=plus " + "into plus-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("https://www.google.com/m8/feeds/contacts/default/full")).setDataStoreFactory( dataStoreFactory).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("googleAccount"); } }
public Credential authorize(String clientSecretJSON) throws Exception { GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new StringReader(clientSecretJSON)); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(SheetsScopes.SPREADSHEETS_READONLY)) .build(); return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); } }
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=calendar " Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory).build(); return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user@gmail.com");
/** * 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; }
private GoogleClientSecrets loadGoogleClientSecrets(JsonFactory jsonFactory) throws IOException { URL url = Resources.getResource("client_secret.json"); CharSource inputSupplier = Resources.asCharSource(url, Charsets.UTF_8); return GoogleClientSecrets.load(jsonFactory, inputSupplier.openStream()); }
@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(); } }
private GoogleClientSecrets loadClientSecretFromJson() throws IOException { try (InputStream in = Files.newInputStream(Paths.get(TestProperties.TEST_GMAIL_API_FOLDER, "client_secret.json"))) { return GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); } catch (FileNotFoundException e) { throw new RuntimeException("You need to set up your Gmail API credentials." + System.lineSeparator() + "See docs/development.md section \"Deploying to a staging server\".", e); } }
/** * Creates an authorized Credential object. * * @return an authorized Credential object. * @throws IOException In the event authorization fails. */ private static Credential authorize() throws IOException { // Load client secrets. //InputStream in = CalendarAuth.class.getResourceAsStream("/client_secret.json"); <- incase it breaks, this is still here InputStream in = new FileInputStream(new File("client_secret.json")); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build(); Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); Logger.getLogger().debug("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); //Try to close input stream since I don't think it was ever closed? in.close(); return credential; }
/** 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"); }
new InputStreamReader(CalendarSample.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=gmail " .build(); return new AuthorizationCodeInstalledApp(flow, new 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"); } }
public Credential authorize(String clientSecretJSON) throws Exception { GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new StringReader(clientSecretJSON)); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .build(); return new AuthorizationCodeInstalledApp(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!"); } }