Renan Geraldo
Renan Geraldo

Reputation: 665

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.

Upvotes: 9

Views: 2498

Answers (2)

Dzmitry Bahdanovich
Dzmitry Bahdanovich

Reputation: 1815

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).

Upvotes: 5

wgm
wgm

Reputation: 2188

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.

Upvotes: 2

Related Questions