Android : inApp purchase receipt validation (Part 2)

让人想犯罪 __ 提交于 2019-12-04 22:58:27
Marc Greenstock

Following up on your original question now that I know you're using Java for your server side code, you will want to use the Google API Client Library for Java. I'm not comfortable with Java myself but the documentation is easy to follow.

First of, you need to authenticate, take a look at the example code Google provides. You currently have a JWT (JSON Web Token) but you can keep to the example by instead downloading the p12 certificate file from the same place on the Google API Dashboard.

Your authentication code should look something like this:

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance()

GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
  .setJsonFactory(jsonFactory)
  .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
  .setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
  .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
  .build();

Also don't forget to set your SERVICE_ACCOUNT_EMAIL and store the key.p12 file somewhere accessible to your server.

If you're interested, this is the docs AndroidPublisherScopes.

As I mentioned, my knowledge of Java is limited so please use your disregard any mistakes I have made.

Google have made some code samples available with downloads and links to GitHub repos. I suggest checking those out.

From here you can use the AndroidPublisher.Purchases.Products.Get method to retrieve the details of a purchase. The docs list the signature as following:

AndroidPublisher.Purchases.Products.Get(java.lang.String packageName, java.lang.String productId, java.lang.String token)

For subscriptions use AndroidPublisher.Purchases.Subscriptions.Get.

If you need more help, consider asking a question with the java and google-api-java-client tags.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!