Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate in-app-purchase Android server side Java?

I followed this tutorial: https://medium.com/@infinitesimal_/doing-android-purchase-validation-api-v3-in-java-5c46fc837368

But I can not make it work! All I can get is this:

{
  "code" : 401,
  "errors" : [ {
    "domain" : "androidpublisher",
    "message" : "The current user has insufficient permissions to perform the requested operation.",
    "reason" : "permissionDenied"
  } ],
  "message" : "The current user has insufficient permissions to perform the requested operation."
}

This is my java code:

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();    

    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();        
    String applicationName = "My App";       
    String packageName = "com.my.package";
    final Set<String> scopes = Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER);
    GoogleCredential credential = new GoogleCredential.Builder()                    
        .setTransport(httpTransport)                        
        .setJsonFactory(jsonFactory)                    
        .setServiceAccountId("myAccoutId")                     
        .setServiceAccountScopes(scopes)                    
        .setServiceAccountPrivateKeyFromP12File(
          new File("/my/path"))                    
        .build();

    AndroidPublisher pub = new AndroidPublisher.Builder
            (httpTransport, jsonFactory, credential)                     
            .setApplicationName(applicationName)                    
            .build();            
        final AndroidPublisher.Purchases.Products.Get get = 
            pub.purchases()
            .products()                    
            .get(packageName, "name", "transactionId"); 
        final ProductPurchase purchase = get.execute();            
        System.out.println("Found google purchase item " + purchase.toPrettyString());

I created a service account and gave the owner role. I went to the Google play console, and in Users and Permissions I also let this service account as administrator.

like image 511
Renan Geraldo Avatar asked Oct 16 '18 23:10

Renan Geraldo


People also ask

How to validate in-app purchase Android?

When a user makes an in-app purchase, cache the details (token, order id, and product id) locally on the client (i.e the app) then send it to your API. Your API should then send the purchaseToken to the Google Play Developer API for validation.


2 Answers

We used the same example, and made a bunch of changes to make it work:

  1. Instead of application name, use package name: replace .setApplicationName(applicationName) with .setApplicationName(packageName)

  2. Instead of P12, we use JSON key: GoogleCredential credential = GoogleCredential.fromStream(IOUtils.toInputStream(jsonCert));

where jsonCert is a JSON Key (Service Accounts -> Select an account -> Create key) looks like

{
"type": "service_account",
"project_id": "api-xxx",
"private_key_id": "xxx",
"private_key": "your_private_key",
"client_email": "[email protected]",
"client_id": "xxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/api-server-service-account%40api-xxx.iam.gserviceaccount.com"
};

Update: as per @Q Locker, it might take some time for Google to provision a service account after creation ( up 2 days in @Q Locker's case).

like image 127
Dzmitry Bahdanovich Avatar answered Oct 24 '22 11:10

Dzmitry Bahdanovich


Dmitry Bogdanovich's answer does not help.

Service account creation does not take effect immediately, in my case, I waited less than 2 days to make it work.

like image 43
Run Avatar answered Oct 24 '22 09:10

Run