Java Code Examples for com.google.api.client.util.SecurityUtils

The following are top voted examples for showing how to use com.google.api.client.util.SecurityUtils. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: typhon   File: GooglePlaySample.java View source code 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();

        PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(
                SecurityUtils.getPkcs12KeyStore(),
                new FileInputStream(new File("{P12 key file}")), // ???P12??
                "notasecret", "privatekey", "notasecret");

        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(transport).setJsonFactory(JacksonFactory.getDefaultInstance())
                .setServiceAccountId("{Email address}") // e.g.: [email protected]ccount.com
                .setServiceAccountScopes(AndroidPublisherScopes.all())
                .setServiceAccountPrivateKey(privateKey).build();

        AndroidPublisher publisher = new AndroidPublisher.Builder(transport,
                JacksonFactory.getDefaultInstance(), credential).build();

        AndroidPublisher.Purchases.Products products = publisher.purchases().products();

        // ??????: https://developers.google.com/android-publisher/api-ref/purchases/products/get
        AndroidPublisher.Purchases.Products.Get product = products.get("{packageName}",
                "{productId}", "{token}");

        // ??????
        // ??????: https://developers.google.com/android-publisher/api-ref/purchases/products
        // ??consumptionState, purchaseState?????????
        ProductPurchase purchase = product.execute();
    }
 
Example 2
Project: typhon   File: GooglePlayRechargingHandler.java View source code 6 votes vote down vote up
public GooglePlayRechargingHandler() {
    try {
        transport = GoogleNetHttpTransport.newTrustedTransport();

        PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(
                SecurityUtils.getPkcs12KeyStore(),
                getClass().getClassLoader().getResourceAsStream("eagle7.p12"),
                "notasecret", "privatekey", "notasecret");

        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(transport).setJsonFactory(JacksonFactory.getDefaultInstance())
                .setServiceAccountId("[email protected]ccount.com")
                .setServiceAccountScopes(AndroidPublisherScopes.all())
                .setServiceAccountPrivateKey(privateKey).build();

        publisher = new AndroidPublisher.Builder(transport,
                JacksonFactory.getDefaultInstance(), credential).build();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 3
Project: cloudbreak   File: GcpStackUtil.java View source code 6 votes vote down vote up
public static GoogleCredential buildCredential(CloudCredential gcpCredential, HttpTransport httpTransport) throws IOException, GeneralSecurityException {
    PrivateKey pk = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
            new ByteArrayInputStream(Base64.decodeBase64(getServiceAccountPrivateKey(gcpCredential))), "notasecret", "privatekey", "notasecret");
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(getServiceAccountId(gcpCredential))
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKey(pk)
            .build();
    return credential;
}
 
Example 4
Project: wallride   File: GoogleAnalyticsUtils.java View source code 6 votes vote down vote up
public static Analytics buildClient(GoogleAnalytics googleAnalytics) {
	Analytics analytics;
	try {
		PrivateKey privateKey= SecurityUtils.loadPrivateKeyFromKeyStore(
				SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(googleAnalytics.getServiceAccountP12FileContent()),
				"notasecret", "privatekey", "notasecret");

		HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
		JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

		Set<String> scopes = new HashSet<>();
		scopes.add(AnalyticsScopes.ANALYTICS_READONLY);

		final GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
				.setJsonFactory(jsonFactory)
				.setServiceAccountId(googleAnalytics.getServiceAccountId())
				.setServiceAccountScopes(scopes)
				.setServiceAccountPrivateKey(privateKey)
				.build();

		HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
			@Override
			public void initialize(HttpRequest httpRequest) throws IOException {
				credential.initialize(httpRequest);
				httpRequest.setConnectTimeout(3 * 60000);  // 3 minutes connect timeout
				httpRequest.setReadTimeout(3 * 60000);  // 3 minutes read timeout
			}
		};
		analytics = new Analytics.Builder(httpTransport, jsonFactory, httpRequestInitializer)
				.setApplicationName("WallRide")
				.build();
	} catch (Exception e) {
		logger.warn("Failed to synchronize with Google Analytics", e);
		throw new GoogleAnalyticsException(e);
	}

	return analytics;
}
 
Example 5
Project: cloudpelican-lsd   File: BigQuerySinkBolt.java View source code 6 votes vote down vote up
public boolean isValid() {
    projectId = getSinkVar("project_id").trim();
    datasetId = getSinkVar("dataset_id").trim();
    serviceAccountId = getSinkVar("service_account_id").trim();
    pk12KeyBase64 = getSinkVar("pk12base64").trim();

    // Init key
    try {
        byte[] keyBytes = Base64.decodeBase64(pk12KeyBase64);
        ByteArrayInputStream bis = new ByteArrayInputStream(keyBytes);
        pk12 = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), bis, "notasecret", "privatekey", "notasecret");
        LOG.info("Loaded PK12 key");
    } catch (Exception e) {
        LOG.error("Failed to load private key", e);
        return false;
    }

    return !projectId.isEmpty() && !datasetId.isEmpty() && !serviceAccountId.isEmpty();
}
 
Example 6
Project: google-api-java-client   File: GoogleCredential.java View source code 6 votes vote down vote up
/**
 * Sets the private key to use with the service account flow or [email protected] null} for none.
 *
 * <p>
 * Overriding is only supported for the purpose of calling the super implementation and changing
 * the return type, but nothing else.
 * </p>
 *
 * @param p12File input stream to the p12 file (closed at the end of this method in a finally
 *        block)
 */
public Builder setServiceAccountPrivateKeyFromP12File(File p12File)
    throws GeneralSecurityException, IOException {
  serviceAccountPrivateKey = SecurityUtils.loadPrivateKeyFromKeyStore(
      SecurityUtils.getPkcs12KeyStore(), new FileInputStream(p12File), "notasecret",
      "privatekey", "notasecret");
  return this;
}
 
Example 7
Project: s2ap-quickstart-java   File: WobCredentials.java View source code 6 votes vote down vote up
private void generateRsaKey() throws IOException, GeneralSecurityException {
  File file = new File(serviceAccountPrivateKeyPath);
  System.out.println("Key Path: " + file.getAbsolutePath());

  byte[] bytes = ByteStreams.toByteArray(new FileInputStream(file));
  InputStream keyStream = new ByteArrayInputStream(bytes);
  serviceAccountPrivateKey = (RSAPrivateKey) SecurityUtils.loadPrivateKeyFromKeyStore(
      SecurityUtils.getPkcs12KeyStore(), keyStream, "notasecret",
      "privatekey", "notasecret");
}