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.
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.
We used the same example, and made a bunch of changes to make it work:
Instead of application name, use package name: replace .setApplicationName(applicationName)
with .setApplicationName(packageName)
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With