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.google.play.developerapi.publisher.samples.AndroidPublisherHelper.java
License:Open Source License
/** * Authorizes the installed application to access user's protected data. * * @throws java.io.IOException/*from w ww.j a v a2s. c o m*/ * @throws java.security.GeneralSecurityException */ private static Credential authorizeWithInstalledApplication(File secretFile, File authStore) throws IOException { log.info("Authorizing using installed application"); // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(secretFile)); // Ensure file has been filled out. checkClientSecretsFile(clientSecrets); dataStoreFactory = new FileDataStoreFactory(authStore); // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)) .setDataStoreFactory(dataStoreFactory).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(INST_APP_USER_ID); }
From source file:com.google.play.developerapi.samples.AndroidPublisherHelper.java
License:Open Source License
/** * Authorizes the installed application to access user's protected data. * * @throws IOException//from ww w .j a v a 2 s. c o m * @throws GeneralSecurityException */ private static Credential authorizeWithInstalledApplication() throws IOException { log.info("Authorizing using installed application"); // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader( AndroidPublisherHelper.class.getResourceAsStream(RESOURCES_CLIENT_SECRETS_JSON))); // Ensure file has been filled out. checkClientSecretsFile(clientSecrets); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)) .setDataStoreFactory(dataStoreFactory).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(INST_APP_USER_ID); }
From source file:com.google.plus.samples.haikuplus.Authenticate.java
License:Open Source License
/** * Reads in the client_secrets.json file and returns the constructed GoogleClientSecrets * object. This method is called lazily to set the client ID, * client secret, and redirect uri./*from w w w .jav a2s . com*/ * @throws RuntimeException if there is an IOException reading the configuration */ public static synchronized void initClientSecretInfo() { if (clientSecrets == null) { try { Reader reader = new FileReader("client_secrets.json"); clientSecrets = GoogleClientSecrets.load(HaikuPlus.JSON_FACTORY, reader); } catch (IOException e) { throw new RuntimeException("Cannot initialize client secrets", e); } } }
From source file:com.google.pubsub.flic.output.SheetsService.java
License:Apache License
private Sheets authorize() throws Exception { InputStream in = new FileInputStream(new File(System.getenv("GOOGLE_OATH2_CREDENTIALS"))); JsonFactory factory = new JacksonFactory(); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(factory, new InputStreamReader(in)); HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(dataStoreDirectory)); List<String> scopes = Collections.singletonList(SheetsScopes.SPREADSHEETS); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, factory, clientSecrets, scopes).setAccessType("offline").setDataStoreFactory(dataStoreFactory).build(); Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) .authorize("user"); return new Sheets.Builder(transport, factory, credential).setApplicationName(APPLICATION_NAME).build(); }
From source file:com.google.sample.games.PlayerServlet.java
License:Apache License
/** * Exchanges the authcode for an access token credential. The credential * is the associated with the given player. * * @param authCode - the non-null authcode passed from the client. * @param player - the player object which the given authcode is * associated with./*from www . j a v a2 s . co m*/ * @return the HTTP response code indicating the outcome of the exchange. */ private int exchangeAuthCode(String authCode, Player player) { try { // The client_secret.json file is downloaded from the Google API // console. This is used to identify your web application. The // contents of this file should not be shared. // // For the sample, this file is expected to be in the root of the // sample (at the same level as the top level build.gradle file). File secretFile = new File("client_secret.json"); // If we don't have the file, we can't access any APIs, so return // an error. if (!secretFile.exists()) { log("Secret file : " + secretFile.getAbsolutePath() + " does not exist!"); return HttpServletResponse.SC_FORBIDDEN; } GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new FileReader(secretFile)); // For the sample server, make sure that the client secret file // has been updated with actual values. if (clientSecrets.getDetails().getClientId().equals("ReplaceMe")) { String message = "client_secret.json is not configured " + "correctly! Download your app's information and place " + "it in client_secret.json"; log("ERROR: " + message); throw new IllegalStateException(message); } // small hack here to extract the application id of the game from // the client id. String applicationId = extractApplicationId(clientSecrets.getDetails().getClientId()); GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(HTTPTransport, JacksonFactory.getDefaultInstance(), "https://www.googleapis.com/oauth2/v4/token", clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret(), authCode, "").execute(); log("hasRefresh == " + (tokenResponse.getRefreshToken() != null)); log("Exchanging authCode: " + authCode + " for token"); Credential credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setJsonFactory(JacksonFactory.getDefaultInstance()).setTransport(HTTPTransport) .setTokenServerEncodedUrl("https://www.googleapis" + ".com/oauth2/v4/token") .setClientAuthentication(new HttpExecuteInterceptor() { @Override public void intercept(HttpRequest request) throws IOException { } }).build().setFromTokenResponse(tokenResponse); player.setCredential(credential); // Now that we have a credential, we can access the Games API. PlayGamesAPI api = new PlayGamesAPI(player, applicationId, HTTPTransport, JacksonFactory.getDefaultInstance()); // Call the verify method, which checks that the access token has // access to the Games API, and that the player id used by the // client matches the playerId associated with the accessToken. boolean ok = api.verifyPlayer(); // This does not add much that is not available on the client, but // is used to demonstrate calling a Games API on the server. if (ok) { ok = api.updatePlayerInfo(); if (ok) { // persist the player. savePlayer(api.getPlayer()); } } return ok ? HttpServletResponse.SC_OK : HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } catch (IOException e) { e.printStackTrace(); } return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; }
From source file:com.google.sites.liberation.export.Main.java
License:Apache License
/** Authorizes the installed application to access user's protected data. * @throws Exception *///from w w w . jav a2s . c o m 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"); }
From source file:com.google.youtube.gaming.chat.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 clientSecret the client secret from Google API console * @param credentialDatastore name of the credential datastore to cache OAuth tokens *///from w w w . j ava 2 s. co m public static Credential authorize(Collection<String> scopes, String clientSecret, String credentialDatastore) throws IOException { // Load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new StringReader(clientSecret)); // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore} FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(getCredentialsDirectory())); DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }
From source file:com.imos.sample.CalendarQuickstart.java
/** * Creates an authorized Credential object. * * @return an authorized Credential object. * @throws IOException/*from w w w . j av a2 s . co m*/ */ public static Credential authorize() throws IOException { // Load client secrets. InputStream in = CalendarQuickstart.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:com.imos.sample.DriveQuickstart.java
/** * Creates an authorized Credential object. * * @return an authorized Credential object. * @throws IOException/*from w w w . ja va2 s .co m*/ */ public static Credential authorize() throws IOException { // Load client secrets. InputStream in = DriveQuickstart.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:com.knsi.DriveQuickstart.java
/** * Creates an authorized Credential object. * @return an authorized Credential object. * @throws IOException//w w w . j a v a2 s . co m */ public static Credential authorize() throws IOException { // Load client secrets. FileInputStream ins = new FileInputStream(new java.io.File("client_secret.json")); //InputStream in = // DriveQuickstart.class.getResourceAsStream("/client_secret.json"); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(ins)); // Build flow and trigger user authorization request. /*thia portoikn of the code determines the portion of the code that actually appears on the user browser*/ 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; }