适用于Java的AndroidPublisher(V3)Google API客户端库的示例 [英] Samples for the AndroidPublisher (V3) Google API Client Library for Java

查看:915
本文介绍了适用于Java的AndroidPublisher(V3)Google API客户端库的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的安全后端服务器的AndroidPublisher库从v1升级到v3. AndroidPublisher库(v3)将允许我安全地进行服务器端购买验证以及对应用内购买和订阅的确认.

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.

v1的现有代码不再兼容. v3库看起来更好,但是我找不到以下示例代码:

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

  1. 使用客户端JSON构建客户端凭据.
  2. Get& Acknowledge使用PurchaseToken进行的购买.
  1. Build client credential using client JSON.
  2. Get & Acknowledge Purchases using PurchaseToken.

Maven设置:

<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文档: https://developers.google. com/android-publisher/api-ref/purchases/products/get

正在寻找这样的示例: https://developers.google.com/api-client-library/java/google-api-java-client/samples

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.

推荐答案

添加以下依赖项

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

这篇关于适用于Java的AndroidPublisher(V3)Google API客户端库的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆