Samples for the AndroidPublisher (V3) Google API Client Library for Java

流过昼夜 提交于 2019-12-11 07:47:59

问题


I am upgrading AndroidPublisher library from v1 to v3 for my secure backend server. AndroidPublisher library (v3) will allows me to do server side purchase validation and acknowledgement for in-app purchases and subscription securely.

Existing code for v1 is not compatible anymore. The v3 library looks better but I could not find any sample code for:

  1. Build client credential using client JSON.
  2. Get & Acknowledge Purchases using PurchaseToken.

Maven setting:

<project>
  <dependencies>
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-androidpublisher</artifactId>
      <version>v3-rev103-1.25.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.30.2</version>
    </dependency>
  </dependencies>
</project>

API documentation: https://developers.google.com/android-publisher/api-ref/purchases/products/get

Looking for a sample like this: https://developers.google.com/api-client-library/java/google-api-java-client/samples

Any short sample code would be a great help.


回答1:


Add following dependencies

compile "com.google.apis:google-api-services-androidpublisher:v3-rev103-1.25.0"
compile "com.google.auth:google-auth-library-oauth2-http:0.17.1"

Then enable Google Developer Play Api library from Google Cloud Console here

In credentials Create service account key with role Pub/Sub Admin and save json file

In Google developers Console here. In Settings >> Developer Account >> API Access link your app.

Then in code do following to get subscription info.

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("<service-account-key-file>.json")).createScoped(AndroidPublisherScopes.ANDROIDPUBLISHER);

AndroidPublisher pub = new AndroidPublisher.Builder(
        GoogleNetHttpTransport.newTrustedTransport(),
        JacksonFactory.getDefaultInstance(),
        new HttpCredentialsAdapter(credentials)
).setApplicationName("<app-name>").build();
SubscriptionPurchase purchase = pub.purchases().subscriptions().get(
        "<app-package>",
        "<subscription-id>",
        "<purchase-token>"
).execute();
println(purchase);

Note: if you get this error:

The current user has insufficient permissions to perform the requested operation.

Then try again after one day because after enable google play developer api and creating service account key from google console you need to wait 24 to 48 hours and also do following

From Google Play Console go to (with admin access):

  1. Setting (Left Panel) Developer Account (Left Panel) Users & permissions (Left Panel)
  2. Click on the Invite New User button.
  3. Enter the service account email (the same you have in the json file you are using and that google generated when you created the service account)
  4. Select Administrator in the Role DropDown menu.
  5. Click on SEND INVITATION


来源:https://stackoverflow.com/questions/57697597/samples-for-the-androidpublisher-v3-google-api-client-library-for-java

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