List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleClientSecrets load
public static GoogleClientSecrets load(JsonFactory jsonFactory, Reader reader) throws IOException
From source file:custom.application.login.java
License:Apache License
public String oAuth2callback() throws ApplicationException { HttpServletRequest request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST"); HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE"); Reforward reforward = new Reforward(request, response); TokenResponse oauth2_response;//from w w w . ja va2s. com try { if (this.getVariable("google_client_secrets") == null) { clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(login.class.getResourceAsStream("/clients_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/ "); } this.setVariable(new ObjectVariable("google_client_secrets", clientSecrets)); } else clientSecrets = (GoogleClientSecrets) this.getVariable("google_client_secrets").getValue(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, SCOPES).build(); oauth2_response = flow.newTokenRequest(request.getParameter("code")) .setRedirectUri(this.getLink("oauth2callback")).execute(); System.out.println("Ok:" + oauth2_response.toPrettyString()); } catch (IOException e1) { // TODO Auto-generated catch block throw new ApplicationException(e1.getMessage(), e1); } catch (GeneralSecurityException e) { // TODO Auto-generated catch block throw new ApplicationException(e.getMessage(), e); } try { HttpClient httpClient = new DefaultHttpClient(); String url = "https://www.google.com/m8/feeds/contacts/default/full"; url = "https://www.googleapis.com/oauth2/v1/userinfo"; HttpGet httpget = new HttpGet(url + "?access_token=" + oauth2_response.getAccessToken()); httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8"); HttpResponse http_response = httpClient.execute(httpget); HeaderIterator iterator = http_response.headerIterator(); while (iterator.hasNext()) { Header next = iterator.nextHeader(); System.out.println(next.getName() + ":" + next.getValue()); } InputStream instream = http_response.getEntity().getContent(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len = instream.read(bytes)) != -1) { out.write(bytes, 0, len); } instream.close(); out.close(); Struct struct = new Builder(); struct.parse(new String(out.toByteArray(), "utf-8")); this.usr = new User(); this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue()); if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) { usr.setPassword(""); usr.setUsername(usr.getEmail()); usr.setLastloginIP(request.getRemoteAddr()); usr.setLastloginTime(new Date()); usr.setRegistrationTime(new Date()); usr.append(); } new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId()); reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8")); reforward.forward(); return new String(out.toByteArray(), "utf-8"); } catch (ClientProtocolException e) { throw new ApplicationException(e.getMessage(), e); } catch (IOException e) { throw new ApplicationException(e.getMessage(), e); } catch (ParseException e) { throw new ApplicationException(e.getMessage(), e); } }
From source file:de.cgawron.godrive.GoDriveServlet.java
License:Apache License
/** * Reads client_secrets.json and creates a GoogleClientSecrets object. * /* w w w. java 2 s .c om*/ * @return A GoogleClientsSecrets object. */ private GoogleClientSecrets getClientSecrets() { if (secrets == null) { // TODO: do not read on each request InputStream stream = getServletContext().getResourceAsStream(CLIENT_SECRETS_FILE_PATH); try { secrets = GoogleClientSecrets.load(JSON_FACTORY, stream); } catch (IOException e) { throw new RuntimeException("No client_secrets.json found"); } } return secrets; }
From source file:de.jlo.talendcomp.google.adwords.AdWordsReport.java
License:Apache License
/** * Authorizes the installed application to access user's protected YouTube * data./* ww w . j a v a 2 s . co m*/ * * @param scopes * list of scopes needed to access general and analytic YouTube * info. */ private Credential authorizeWithClientSecret() throws Exception { info("Authorise with Client-ID for installed application with using credential data store...."); if (clientSecretFile == null) { throw new IllegalStateException("client secret file is not set"); } File secretFile = new File(clientSecretFile); if (secretFile.exists() == false) { throw new Exception( "Client secret file:" + secretFile.getAbsolutePath() + " does not exists or is not readable."); } Reader reader = new FileReader(secretFile); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader); try { reader.close(); } catch (Throwable e) { } // Checks that the defaults have been replaced (Default = // "Enter X here"). if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { throw new Exception("The client secret file does not contains the credentials!"); } String credentialDataStoreDir = secretFile.getParent() + "/" + clientSecrets.getDetails().getClientId() + "/"; File credentialDataStoreDirFile = new File(credentialDataStoreDir); if (credentialDataStoreDirFile.exists() == false && credentialDataStoreDirFile.mkdirs() == false) { throw new Exception( "Credentedial data dir does not exists or cannot created:" + credentialDataStoreDir); } if (debug) { info("Credential data store dir:" + credentialDataStoreDir); } FileDataStoreFactory fdsf = new FileDataStoreFactory(credentialDataStoreDirFile); // Set up authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Arrays.asList(ADWORDS_SCOPE)).setDataStoreFactory(fdsf).setClock(new Clock() { @Override public long currentTimeMillis() { // we must be sure, that we are always in the past from Googles point of view // otherwise we get an "invalid_grant" error return System.currentTimeMillis() - timeMillisOffsetToPast; } }).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(userEmail); }
From source file:de.jlo.talendcomp.google.analytics.GoogleAnalyticsBase.java
License:Apache License
/** * Authorizes the installed application to access user's protected YouTube * data./*w w w . j a v a2 s . c om*/ * * @param scopes * list of scopes needed to access general and analytic YouTube * info. */ private Credential authorizeWithClientSecret() throws Exception { if (clientSecretFile == null) { throw new IllegalStateException("client secret file is not set"); } File secretFile = new File(clientSecretFile); if (secretFile.exists() == false) { throw new Exception( "Client secret file:" + secretFile.getAbsolutePath() + " does not exists or is not readable."); } Reader reader = new FileReader(secretFile); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader); try { reader.close(); } catch (Throwable e) { } // Checks that the defaults have been replaced (Default = // "Enter X here"). if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { throw new Exception( "The client secret file does not contains the credentials. At first you have to pass the web based authorization process!"); } credentialDataStoreDir = secretFile.getParent() + "/" + clientSecrets.getDetails().getClientId() + "/"; File credentialDataStoreDirFile = new File(credentialDataStoreDir); if (credentialDataStoreDirFile.exists() == false && credentialDataStoreDirFile.mkdirs() == false) { throw new Exception( "Credentedial data dir does not exists or cannot created:" + credentialDataStoreDir); } FileDataStoreFactory fdsf = new FileDataStoreFactory(credentialDataStoreDirFile); // Set up authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY)).setDataStoreFactory(fdsf) .setClock(new Clock() { @Override public long currentTimeMillis() { // we must be sure, that we are always in the past from Googles point of view // otherwise we get an "invalid_grant" error return System.currentTimeMillis() - timeMillisOffsetToPast; } }).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(accountEmail); }
From source file:de.jlo.talendcomp.google.analytics.unsampled.UnsampledReportHelper.java
License:Apache License
/** * Authorizes the installed application to access user's protected YouTube * data./*from w w w . j av a2 s . c o m*/ * * @param scopes * list of scopes needed to access general and analytic YouTube * info. */ private Credential authorizeWithClientSecret() throws Exception { if (clientSecretFile == null) { throw new IllegalStateException("client secret file is not set"); } File secretFile = new File(clientSecretFile); if (secretFile.exists() == false) { throw new Exception( "Client secret file:" + secretFile.getAbsolutePath() + " does not exists or is not readable."); } Reader reader = new FileReader(secretFile); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader); try { reader.close(); } catch (Throwable e) { } // Checks that the defaults have been replaced (Default = // "Enter X here"). if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { throw new Exception( "The client secret file does not contains the credentials. At first you have to pass the web based authorization process!"); } credentialDataStoreDir = secretFile.getParent() + "/" + clientSecrets.getDetails().getClientId() + "/"; File credentialDataStoreDirFile = new File(credentialDataStoreDir); if (credentialDataStoreDirFile.exists() == false && credentialDataStoreDirFile.mkdirs() == false) { throw new Exception( "Credentedial data dir does not exists or cannot created:" + credentialDataStoreDir); } FileDataStoreFactory fdsf = new FileDataStoreFactory(credentialDataStoreDirFile); List<String> scopes = null; if (useEditScope) { scopes = Arrays.asList(AnalyticsScopes.ANALYTICS, AnalyticsScopes.ANALYTICS_EDIT); } else { scopes = Arrays.asList(AnalyticsScopes.ANALYTICS); } // Set up authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setDataStoreFactory(fdsf).setClock(new Clock() { @Override public long currentTimeMillis() { // we must be sure, that we are always in the past from Googles point of view // otherwise we get an "invalid_grant" error return System.currentTimeMillis() - timeMillisOffsetToPast; } }).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(accountEmail); }
From source file:de.jlo.talendcomp.google.analytics.uploads.UploadHelper.java
License:Apache License
/** * Authorizes the installed application to access user's protected YouTube * data./*from w w w . ja v a2s . co m*/ * * @param scopes * list of scopes needed to access general and analytic YouTube * info. */ private Credential authorizeWithClientSecret() throws Exception { if (clientSecretFile == null) { throw new IllegalStateException("client secret file is not set"); } File secretFile = new File(clientSecretFile); if (secretFile.exists() == false) { throw new Exception( "Client secret file:" + secretFile.getAbsolutePath() + " does not exists or is not readable."); } Reader reader = new FileReader(secretFile); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader); try { reader.close(); } catch (Throwable e) { } // Checks that the defaults have been replaced (Default = // "Enter X here"). if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { throw new Exception( "The client secret file does not contains the credentials. At first you have to pass the web based authorization process!"); } credentialDataStoreDir = secretFile.getParent() + "/" + clientSecrets.getDetails().getClientId() + "/"; File credentialDataStoreDirFile = new File(credentialDataStoreDir); if (credentialDataStoreDirFile.exists() == false && credentialDataStoreDirFile.mkdirs() == false) { throw new Exception( "Credentedial data dir does not exists or cannot created:" + credentialDataStoreDir); } FileDataStoreFactory fdsf = new FileDataStoreFactory(credentialDataStoreDirFile); // Set up authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Arrays.asList(AnalyticsScopes.ANALYTICS)).setDataStoreFactory(fdsf) .setClock(new Clock() { @Override public long currentTimeMillis() { // we must be sure, that we are always in the past from Googles point of view // otherwise we get an "invalid_grant" error return System.currentTimeMillis() - timeMillisOffsetToPast; } }).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(accountEmail); }
From source file:de.jlo.talendcomp.google.drive.DriveHelper.java
License:Apache License
private Credential authorizeWithClientSecret() throws Exception { if (clientSecretFile == null) { throw new IllegalStateException("client secret file is not set"); }/* www. j a v a 2 s . c o m*/ File secretFile = new File(clientSecretFile); if (secretFile.exists() == false) { throw new Exception( "Client secret file:" + secretFile.getAbsolutePath() + " does not exists or is not readable."); } Reader reader = new FileReader(secretFile); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader); try { reader.close(); } catch (Throwable e) { } // Checks that the defaults have been replaced (Default = // "Enter X here"). if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { throw new Exception( "The client secret file does not contains the credentials. At first you have to pass the web based authorization process!"); } credentialDataStoreDir = secretFile.getParent() + "/" + clientSecrets.getDetails().getClientId() + "/"; File credentialDataStoreDirFile = new File(credentialDataStoreDir); if (credentialDataStoreDirFile.exists() == false && credentialDataStoreDirFile.mkdirs() == false) { throw new Exception( "Credentedial data dir does not exists or cannot created:" + credentialDataStoreDir); } FileDataStoreFactory fdsf = new FileDataStoreFactory(credentialDataStoreDirFile); // Set up authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Arrays.asList(DriveScopes.DRIVE, DriveScopes.DRIVE_FILE)).setDataStoreFactory(fdsf) .setClock(new Clock() { @Override public long currentTimeMillis() { // we must be sure, that we are always in the past from Googles point of view // otherwise we get an "invalid_grant" error return System.currentTimeMillis() - timeMillisOffsetToPast; } }).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(accountEmail); }
From source file:de.jlo.talendcomp.youtubeanalytics.YoutubeAnalyticsInput.java
License:Apache License
/** * Authorizes the installed application to access user's protected YouTube * data.// ww w. ja va 2 s . c o m * * @param scopes * list of scopes needed to access general and analytic YouTube * info. */ private Credential authorizeWithClientSecret() throws Exception { if (clientSecretFile == null) { throw new IllegalStateException("client secret file is not set"); } File secretFile = new File(clientSecretFile); if (secretFile.exists() == false) { throw new Exception( "Client secret file:" + secretFile.getAbsolutePath() + " does not exists or is not readable."); } Reader reader = new FileReader(secretFile); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader); try { reader.close(); } catch (Throwable e) { } // Checks that the defaults have been replaced (Default = // "Enter X here"). if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { throw new Exception("The client secret file does not contains the credentials!"); } credentialDataStoreDir = secretFile.getParent() + "/" + clientSecrets.getDetails().getClientId() + "/"; File credentialDataStoreDirFile = new File(credentialDataStoreDir); if (credentialDataStoreDirFile.exists() == false && credentialDataStoreDirFile.mkdirs() == false) { throw new Exception( "Credentedial data dir does not exists or cannot created:" + credentialDataStoreDir); } if (debug) { System.out.println("Credential data store dir:" + credentialDataStoreDir); } FileDataStoreFactory fdsf = new FileDataStoreFactory(credentialDataStoreDirFile); // Set up authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Arrays.asList(YouTubeAnalyticsScopes.YT_ANALYTICS_READONLY)) .setDataStoreFactory(fdsf).setClock(new Clock() { @Override public long currentTimeMillis() { // we must be sure, that we are always in the past from Googles point of view // otherwise we get an "invalid_grant" error return System.currentTimeMillis() - timeMillisOffsetToPast; } }).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(accountEmail); }
From source file:de.pfabulist.googledrive.GoogleDriveElsewhere.java
License:BSD License
/** * Authorizes the installed application to access user's protected data. *//* ww w .ja va 2s .c o m*/ private Credential authorize(InputStream jsonIS) throws Exception { // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(jsonIS)); 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=drive " + "into drive-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(DriveScopes.DRIVE_FILE)).setDataStoreFactory(dataStoreFactory) .build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }
From source file:de.smilix.gaeCalenderGateway.service.auth.AuthService.java
License:Apache License
private GoogleClientSecrets getClientCredential() throws AuthException { if (this.clientSecrets == null) { InputStream ressourceStream = AuthService.class.getResourceAsStream(CLIENT_SECRETS_JSON); if (ressourceStream == null) { throw new AuthException("No '" + CLIENT_SECRETS_JSON + "' file found."); }/* www. j a v a2s. c om*/ try { this.clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(ressourceStream)); } catch (IOException e) { throw new AuthException("Error loading '" + CLIENT_SECRETS_JSON + "' file: " + e.getMessage(), e); } if (Utils.isEmpty(this.clientSecrets.getDetails().getClientId()) || Utils.isEmpty(this.clientSecrets.getDetails().getClientSecret())) { throw new AuthException("The '" + CLIENT_SECRETS_JSON + "' file is incomplete."); } } return this.clientSecrets; }