且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

适用于Java的AndroidPublisher(V3)Google API客户端库的示例

更新时间:2023-02-14 14:11:47

添加以下依赖项

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"

然后从Google Cloud Console 此处启用Google Developer Play Api库

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

使用凭据创建具有角色Pub/Sub Admin的服务帐户密钥并保存json文件

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

在Google开发者控制台中此处. 在Settings >> Developer Account >> API Access中,链接您的应用.

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.

然后在一天后重试,因为在启用Google Play开发者API并从Google控制台创建服务帐户密钥后,您需要等待24到48小时,然后执行以下操作

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

从Google Play控制台转到(具有管理员访问权限):

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

  1. 设置(左面板)开发人员帐户(左面板)用户和 权限(左面板)
  2. 单击邀请新用户"按钮.
  3. 输入服务帐户电子邮件(与json文件中的电子邮件相同) 您正在使用,并且在创建服务时由Google生成 帐户)
  4. 在角色"下拉菜单中选择管理员".
  5. 点击 发送邀请
  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