Am I getting the steps right for verifying a user's Android in-app subscription?

前端 未结 7 1441
刺人心
刺人心 2020-11-28 21:06

I am making an app that does not require a user account/login, and allows the user to purchase a subscription. I want to use the Google Play Developer API to verify whether

相关标签:
7条回答
  • 2020-11-28 22:05

    I don't know in 2012, but in 2015 you should not do any of these steps manually. I had a very hard time to find the documentation so I am posting here in case it helps anyone.

    1. You should only query in-app purchases from your server for security reasons as otherwise you can trust none of the 2 ends of the purchase process.

    Now on the server side (I think you could still use the same code from your app if you absolutely need to), include the google-api-services-androidpublisher client library to your project (see https://developers.google.com/api-client-library/java/apis/androidpublisher/v1)

    As you mentioned, you need a service account with a P12 file (the client library only accept P12 file).

    Then the following code will authenticate and get purchase information nicely:

        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = new JacksonFactory();
    
        List<String> scopes = new ArrayList<String>();
        scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);
    
        Credential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory)
                .setServiceAccountId(googleServiceAccountId)
                .setServiceAccountPrivateKeyFromP12File(new File(googleServicePrivateKeyPath))
                .setServiceAccountScopes(scopes).build();
        AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).build();
        AndroidPublisher.Purchases purchases = publisher.purchases();
        final Get request = purchases.get(packageName, productId, token);
        final SubscriptionPurchase purchase = request.execute();
    
        // Do whatever you want with the purchase bean
    

    Information on Java client authentication can be found here: https://developers.google.com/identity/protocols/OAuth2ServiceAccount

    0 讨论(0)
提交回复
热议问题