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:com.utilties.util.gmail.Utils.java
License:Apache License
private static GoogleClientSecrets getClientSecrets() throws IOException { if (clientSecrets == null) { clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(Utils.class.getResourceAsStream("/client_secret.json"))); /* Preconditions.checkArgument(!clientSecrets.getDetails().getClientId().startsWith("Enter ") && !clientSecrets.getDetails().getClientSecret().startsWith("Enter "), "Download client_secrets.json file from https://code.google.com/apis/console/?api=plus " + "into plus-appengine-sample/src/main/resources/client_secrets.json");*/ }//from w w w . ja v a 2 s . com return clientSecrets; }
From source file:com.winster.routemarks.rest.google.ServiceUtils.java
License:Apache License
static GoogleClientSecrets getClientCredential() throws IOException { if (clientSecrets == null) { clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, ServiceUtils.class.getResourceAsStream("/client_secrets.json")); Preconditions.checkArgument(/* w w w .j a va 2 s . com*/ !clientSecrets.getDetails().getClientId().startsWith("Enter ") && !clientSecrets.getDetails().getClientSecret().startsWith("Enter "), "Enter Client ID and Secret from https://code.google.com/apis/console/?api=bigquery " + "into bigquery-appengine-sample/src/main/resources/client_secrets.json"); } return clientSecrets; }
From source file:com.wiwit.eplweb.util.youtube.api.Auth.java
License:Apache License
/** * Authorizes the installed application to access user's protected data. * * @param scopes list of scopes needed to run youtube upload. * @param credentialDatastore name of the credential datastore to cache OAuth tokens */// ww w .j a v a 2 s .c om public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException { // Load client secrets. InputStream is = new FileInputStream(WebappProps.getClientScret()); Reader clientSecretReader = new InputStreamReader(is); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader); // This creates the credentials datastore File f = new File(WebappProps.getGoogleCredentialsDirectory()); FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(f); DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).setAccessType("offline") .setApprovalPrompt("force").build(); // Build the local server and bind it to port 8080 LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(9001).build(); // Authorize. return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user"); }
From source file:com.wut.datasource.GoogleAnalyticsDataSource.java
License:Apache License
/** 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(AnalyticsSample.class.getResourceAsStream("/client_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out//from w ww.j av a 2 s . c om .println("Enter Client ID and Secret from https://code.google.com/apis/console/?api=analytics " + "into analytics-cmdline-sample/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)) .setDataStoreFactory(DATA_STORE_FACTORY).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }
From source file:com.yagasoft.overcast.implement.google.Authorisation.java
License:MIT License
/** * @see com.yagasoft.overcast.base.csp.authorisation.OAuth#acquirePermission() *//* w ww. java 2 s. com*/ @Override public void acquirePermission() throws AuthorisationException { Logger.info("GOOGLE: AUTH: started ..."); // authorise try { // the folder where Google API stores creds. dataStoreFolder = tokenParent; dataStoreFactory = new FileDataStoreFactory(dataStoreFolder.toFile()); // load the JSON containing info required for identifying the dev account. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(Google.JSON_FACTORY, new InputStreamReader(Files.newInputStream(infoFile))); // dev info contained in the JSON. String clientId = clientSecrets.getDetails().getClientId(); String clientSecret = clientSecrets.getDetails().getClientSecret(); // problem?! if (clientId.startsWith("Enter") || clientSecret.startsWith("Enter")) { Logger.error( "GOOGLE: AUTH: Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive " + "into google_secrets.json"); throw new AuthorisationException("Failed to authorise!"); } // set up authorisation code flow flow = new GoogleAuthorizationCodeFlow.Builder(Google.httpTransport, Google.JSON_FACTORY, clientSecrets, Collections.singleton(DriveScopes.DRIVE)) .setCredentialDataStore(getDataStore(dataStoreFactory, "google")) .setAccessType("offline").build(); credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(userID); Logger.info("Google: AUTH: success"); } catch (IOException e) { Logger.error("Google: AUTH: failed"); Logger.except(e); e.printStackTrace(); throw new AuthorisationException("Failed to authorise! " + e.getMessage()); } }
From source file:com.youtube.apiv3.YoutubeManager.java
public static Credential authorize(String credentialDatastore, InputStream secrets, String user) throws IOException { List<String> scopes = new ArrayList(); scopes.add("https://www.googleapis.com/auth/youtube"); scopes.add("https://www.googleapis.com/auth/youtube.upload"); // Load client secrets. Reader clientSecretReader = new InputStreamReader(secrets); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader); // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore} FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory( new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY)); DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).setApprovalPrompt("force") .setAccessType("offline").build(); // Build the local server and bind it to port 8080 LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8090).build(); // Authorize. System.out.println("Credentials: " + flow.getCredentialDataStore().values()); return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize(user); }
From source file:com.youtube.apiv3.YoutubeManager.java
private static void buildLoginUrl(InputStream secrets) throws IOException { List<String> scopes = new ArrayList(); scopes.add("https://www.googleapis.com/auth/youtube"); scopes.add("https://www.googleapis.com/auth/youtube.upload"); // Load client secrets. Reader clientSecretReader = new InputStreamReader(secrets); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader); // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore} FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory( new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY)); DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("uploadvideo"); flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes) .setCredentialDataStore(datastore).build(); GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl(); loginUrl = url.setRedirectUri(CALLBACK_URI).setAccessType("offline").build(); clientSecretReader.close();/*from ww w .j a v a 2s. c o m*/ secrets.close(); }
From source file:controller.DAOSpreadSheet.java
/** * Creates an authorized Credential object. * @return an authorized Credential object. * @throws IOException//from ww w .j a v a2 s .co m */ private static Credential authorize() throws IOException { // Load client secrets. InputStream in = new FileInputStream("src//files//secret_key.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"); return credential; }
From source file:Controller.ServiceController.java
/** * Creates an authorized Credential object. * @return an authorized Credential object. * @throws IOException//from ww w .j a va 2 s.c o m */ public static Credential authorize() throws IOException { // Load client secrets. InputStream in = ServiceController.class.getResourceAsStream("/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"); System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); return credential; }
From source file:controllers.PlusSample.java
License:Apache License
/** 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 FileReader("C:\\first_play\\app\\installed.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);//from ww w .j a v a 2 s . c o m } // 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"); }