无法从Google Play Android开发者API获取订阅详细信息

时间:2019-09-05 05:51:11

标签: java android google-api-java-client android-inapp-purchase

我正在尝试使用Java的Google API客户端库来获取有关在我的Android应用中购买的用户订阅的信息。然后我得到了这个错误:

  {
    "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."
 }

我正在尝试这些步骤:

  1. 转到https://console.cloud.google.com
  2. 创建项目(或选择现有项目)
  3. 以角色Pub / Sub Admin创建服务帐户
  4. 转到https://console.cloud.google.com/apis/library并搜索“ Google 播放Android开发人员API”
  5. 启用该API
  6. 转到https://play.google.com/apps/publish
  7. 转到“设置”>“开发者帐户”>“ API访问权限”
  8. 链接您在步骤2中创建的项目
  9. 将显示您在第3步中创建的服务帐户
  10. 在游戏控制台中通过应用程序的财务许可授予对它的访问权限
  11. 等待至少24小时,更改才会生效。

我正在尝试此代码:

        httpTransport =  new com.google.api.client.http.javanet.NetHttpTransport();
        JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        String applicationName = "App name";
        String packageName = "com.example.xxx";
        final Set<String> scopes = Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER);

        AssetManager am = getAssets();
        InputStream inputStream = am.open("key1.p12");

        File file = createFileFromInputStream(inputStream);
            Log.d(TAG, "file : " + file);
            if(file != null){
                int SDK_INT = android.os.Build.VERSION.SDK_INT;
                if (SDK_INT > 8) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                            .permitAll().build();
                    StrictMode.setThreadPolicy(policy);


                    GoogleCredential credential = new GoogleCredential.Builder()
                            .setTransport(httpTransport)
                            .setJsonFactory(jsonFactory)
                            .setServiceAccountId("pp-subscription@practice-presto-dev.iam.gserviceaccount.com").setServiceAccountScopes(scopes)
                            //.setServiceAccountId("practice-presto-subscription@practice-presto-dev.iam.gserviceaccount.com").setServiceAccountScopes(scopes)
                            .setServiceAccountPrivateKeyFromP12File(file).build();

                    AndroidPublisher pub = new AndroidPublisher.Builder
                            (httpTransport, jsonFactory, credential)
                            .setApplicationName(applicationName)
                            .build();
                    final AndroidPublisher.Purchases.Subscriptions.Get get =
                            pub.purchases().subscriptions().get(packageName, "sub_monthly_pro", "pcehicpbjhcdnjockiniaokh.AO-J1OxsJtLehF3z_naoXR4LE0jqiXrABAPYiZMNRMZO5jnKI9gnyHmPP7INtcc2kyptNKP_HM6MjEPQfmYWmJ8R_geonsLqMXA9TLsozqNexh-FxSvQFDZSUTgBW_azvdAJPLxPFuKd");
                    final SubscriptionPurchase purchase = get.execute();
                    Log.d(TAG, "Found google purchase item " + purchase.toPrettyString());
                }
            }

我正在测试应用内购买模式。

我想在续订订阅计划后获取最新的订阅收据。

1 个答案:

答案 0 :(得分:0)

24小时后,我检查了错误。但是之后,两天后我再次检查没有错误,并且已经接收到数据。