Search in sources :

Example 1 with AndroidPublisher

use of com.google.api.services.androidpublisher.AndroidPublisher in project OsmAnd-tools by osmandapp.

the class UpdateSubscriptionImpl method getPublisherApi.

private static AndroidPublisher getPublisherApi(String file) throws JSONException, IOException {
    Properties properties = new Properties();
    properties.load(new FileInputStream(file));
    GOOGLE_CLIENT_CODE = properties.getProperty("GOOGLE_CLIENT_CODE");
    GOOGLE_CLIENT_ID = properties.getProperty("GOOGLE_CLIENT_ID");
    GOOGLE_CLIENT_SECRET = properties.getProperty("GOOGLE_CLIENT_SECRET");
    GOOGLE_REDIRECT_URI = properties.getProperty("GOOGLE_REDIRECT_URI");
    TOKEN = properties.getProperty("TOKEN");
    // getRefreshToken();
    String token = TOKEN;
    String accessToken = getAccessToken(token);
    TokenResponse tokenResponse = new TokenResponse();
    // System.out.println("refresh token=" + token);
    // System.out.println("access token=" + accessToken);
    tokenResponse.setAccessToken(accessToken);
    tokenResponse.setRefreshToken(token);
    tokenResponse.setExpiresInSeconds(3600L);
    tokenResponse.setScope("https://www.googleapis.com/auth/androidpublisher");
    tokenResponse.setTokenType("Bearer");
    HttpRequestInitializer credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setClientSecrets(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET).build().setFromTokenResponse(tokenResponse);
    AndroidPublisher publisher = new AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(GOOGLE_PRODUCT_NAME).build();
    return publisher;
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 2 with AndroidPublisher

use of com.google.api.services.androidpublisher.AndroidPublisher in project OsmAnd-tools by osmandapp.

the class UpdateSubscriptionImpl method queryPurchases.

private static void queryPurchases(AndroidPublisher publisher, Connection conn, ResultSet rs) throws SQLException {
    PreparedStatement ps = conn.prepareStatement("INSERT INTO supporters_subscription(userid, sku, purchaseToken, checktime, autorenewing, starttime, expiretime, kind) " + " VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
    PreparedStatement updateStatement = conn.prepareStatement("UPDATE supporters_subscription SET kind=?" + " WHERE userid =?");
    AndroidPublisher.Purchases purchases = publisher.purchases();
    int changes = 0;
    int deletions = 0;
    while (rs.next()) {
        if (rs.getString("kind") != null) {
            if (rs.getString("kind").equals(INVALID_PURCHASE)) {
                continue;
            }
        }
        String userid = rs.getString("userid");
        String pt = rs.getString("purchasetoken");
        String subscriptionId = rs.getString("sku");
        SubscriptionPurchase subscription;
        try {
            if (subscriptionId.startsWith("osm_free")) {
                subscription = purchases.subscriptions().get(GOOGLE_PACKAGE_NAME_FREE, subscriptionId, pt).execute();
            } else {
                subscription = purchases.subscriptions().get(GOOGLE_PACKAGE_NAME, subscriptionId, pt).execute();
            }
        } catch (Exception e) {
            if (!pt.contains(".AO")) {
                updateStatement.setString(1, INVALID_PURCHASE);
                updateStatement.setString(2, userid);
                updateStatement.addBatch();
                deletions++;
                System.out.println("Clearing invalid subscription: userid=" + userid + " sku=" + subscriptionId);
            } else {
                System.err.println("Error updating userid " + userid + " and sku " + subscriptionId);
                e.printStackTrace();
            }
            continue;
        }
        long tm = System.currentTimeMillis();
        ps.setString(1, userid);
        ps.setString(2, subscriptionId);
        ps.setString(3, pt);
        ps.setLong(4, tm);
        ps.setString(5, subscription.getAutoRenewing() + "");
        ps.setLong(6, subscription.getStartTimeMillis());
        ps.setLong(7, subscription.getExpiryTimeMillis());
        ps.setString(8, subscription.getKind());
        System.out.println("Update " + userid + " time " + tm + " expire " + subscription.getExpiryTimeMillis());
        ps.addBatch();
        changes++;
    }
    if (changes > 0) {
        ps.executeBatch();
        if (deletions > 0) {
            updateStatement.executeBatch();
        }
        if (!conn.getAutoCommit()) {
            conn.commit();
        }
    }
}
Also used : AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) SubscriptionPurchase(com.google.api.services.androidpublisher.model.SubscriptionPurchase) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) JSONException(org.json.JSONException)

Example 3 with AndroidPublisher

use of com.google.api.services.androidpublisher.AndroidPublisher in project gradle-play-publisher by ZeroBrain.

the class BasicUploadApk method upload.

public void upload(String applicationName, String packageName, File apkFiles, File secretFile, File authStore, String productType) {
    try {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(packageName), "ApplicationConfig.PACKAGE_NAME cannot be null or empty!");
        // Create the API service.
        AndroidPublisher service = AndroidPublisherHelper.init(applicationName, secretFile, authStore);
        final Edits edits = service.edits();
        // Create a new edit to make changes to your listing.
        Insert editRequest = edits.insert(packageName, null);
        AppEdit edit = editRequest.execute();
        final String editId = edit.getId();
        log.info(String.format("Created edit with id: %s", editId));
        // Upload new apk to developer console
        final AbstractInputStreamContent apkFile = new FileContent(AndroidPublisherHelper.MIME_TYPE_APK, apkFiles);
        Upload uploadRequest = edits.apks().upload(packageName, editId, apkFile);
        Apk apk = uploadRequest.execute();
        log.info(String.format("Version code %d has been uploaded", apk.getVersionCode()));
        // Assign apk to alpha track.
        List<Integer> apkVersionCodes = new ArrayList<Integer>();
        apkVersionCodes.add(apk.getVersionCode());
        Update updateTrackRequest = edits.tracks().update(packageName, editId, productType, new Track().setVersionCodes(apkVersionCodes));
        Track updatedTrack = updateTrackRequest.execute();
        log.info(String.format("Track %s has been updated.", updatedTrack.getTrack()));
        // Commit changes for edit.
        Commit commitRequest = edits.commit(packageName, editId);
        AppEdit appEdit = commitRequest.execute();
        log.info(String.format("App edit with id %s has been comitted", appEdit.getId()));
    } catch (IOException ex) {
        log.error("Excpetion was thrown while uploading apk to production track", ex);
    } catch (GeneralSecurityException ex) {
        log.error("Excpetion was thrown while uploading apk to production track", ex);
    }
}
Also used : AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Upload(com.google.api.services.androidpublisher.AndroidPublisher.Edits.Apks.Upload) IOException(java.io.IOException) Insert(com.google.api.services.androidpublisher.AndroidPublisher.Edits.Insert) Update(com.google.api.services.androidpublisher.AndroidPublisher.Edits.Tracks.Update) AbstractInputStreamContent(com.google.api.client.http.AbstractInputStreamContent) FileContent(com.google.api.client.http.FileContent) Edits(com.google.api.services.androidpublisher.AndroidPublisher.Edits) Commit(com.google.api.services.androidpublisher.AndroidPublisher.Edits.Commit) AppEdit(com.google.api.services.androidpublisher.model.AppEdit) Apk(com.google.api.services.androidpublisher.model.Apk) Track(com.google.api.services.androidpublisher.model.Track)

Example 4 with AndroidPublisher

use of com.google.api.services.androidpublisher.AndroidPublisher in project OsmAnd-tools by osmandapp.

the class UpdateSubscriptionImpl method main.

public static void main(String[] args) throws JSONException, IOException, SQLException, ClassNotFoundException {
    AndroidPublisher publisher = getPublisherApi(args[0]);
    // if(true ){
    // test(publisher, "","");
    // return;
    // }
    Class.forName("org.postgresql.Driver");
    Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5433/changeset", System.getenv("DB_USER"), System.getenv("DB_PWD"));
    boolean verifyAll = false;
    for (int i = 1; i < args.length; i++) {
        if ("-verifyall".equals(args[i])) {
            verifyAll = true;
        }
    }
    ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM ( " + "  SELECT DISTINCT userid, sku,  " + "	first_value(purchaseToken) over (partition by userid, sku order by checktime desc) purchaseToken, " + "	first_value(checktime) over (partition by userid, sku order by checktime desc) checktime, " + "	first_value(time) over (partition by userid, sku order by checktime desc) time_d, " + "	first_value(autorenewing) over (partition by userid, sku order by checktime desc) autorenewing, " + "	first_value(starttime) over (partition by userid, sku order by checktime desc) starttime, " + "	first_value(kind) over (partition by userid, sku order by checktime desc) kind, " + "	first_value(expiretime) over (partition by userid, sku order by checktime desc) expiretime " + "  FROM supporters_subscription ) a " + (verifyAll ? ";" : "WHERE kind = '' or kind is null;"));
    queryPurchases(publisher, conn, rs);
}
Also used : AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 5 with AndroidPublisher

use of com.google.api.services.androidpublisher.AndroidPublisher in project OsmAnd-tools by osmandapp.

the class UpdateSubscriptionImpl method test.

private static void test(AndroidPublisher publisher, String subscriptionId, String purchaseToken) {
    try {
        com.google.api.services.androidpublisher.AndroidPublisher.Inappproducts.List lst = publisher.inappproducts().list(GOOGLE_PACKAGE_NAME_FREE);
        InappproductsListResponse response = lst.execute();
        for (InAppProduct p : response.getInappproduct()) {
            System.out.println("SKU=" + p.getSku() + " type=" + p.getPurchaseType() + " LNG=" + p.getDefaultLanguage() + // " P="+p.getPrices()+
            " Period=" + p.getSubscriptionPeriod() + " Status=" + p.getStatus());
        }
        AndroidPublisher.Purchases purchases = publisher.purchases();
        SubscriptionPurchase subscription = purchases.subscriptions().get(GOOGLE_PACKAGE_NAME_FREE, subscriptionId, purchaseToken).execute();
        System.out.println(subscription.getUnknownKeys());
        System.out.println(subscription.getAutoRenewing());
        System.out.println(subscription.getKind());
        System.out.println(new Date(subscription.getExpiryTimeMillis()));
        System.out.println(new Date(subscription.getStartTimeMillis()));
    // return subscription.getExpiryTimeMillis();
    // return subscripcion.getValidUntilTimestampMsec();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InappproductsListResponse(com.google.api.services.androidpublisher.model.InappproductsListResponse) AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) SubscriptionPurchase(com.google.api.services.androidpublisher.model.SubscriptionPurchase) InAppProduct(com.google.api.services.androidpublisher.model.InAppProduct)

Aggregations

AndroidPublisher (com.google.api.services.androidpublisher.AndroidPublisher)5 SubscriptionPurchase (com.google.api.services.androidpublisher.model.SubscriptionPurchase)2 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 AbstractInputStreamContent (com.google.api.client.http.AbstractInputStreamContent)1 FileContent (com.google.api.client.http.FileContent)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 Edits (com.google.api.services.androidpublisher.AndroidPublisher.Edits)1 Upload (com.google.api.services.androidpublisher.AndroidPublisher.Edits.Apks.Upload)1 Commit (com.google.api.services.androidpublisher.AndroidPublisher.Edits.Commit)1 Insert (com.google.api.services.androidpublisher.AndroidPublisher.Edits.Insert)1 Update (com.google.api.services.androidpublisher.AndroidPublisher.Edits.Tracks.Update)1 Apk (com.google.api.services.androidpublisher.model.Apk)1 AppEdit (com.google.api.services.androidpublisher.model.AppEdit)1 InAppProduct (com.google.api.services.androidpublisher.model.InAppProduct)1 InappproductsListResponse (com.google.api.services.androidpublisher.model.InappproductsListResponse)1 Track (com.google.api.services.androidpublisher.model.Track)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Connection (java.sql.Connection)1