Skip to content
Advertisement

Unable to fetching subscription detail from Google Play Android Developer API

I am trying to use Google APIs Client Library for Java to get information about user’s subscriptions purchased in my android app. Then I got this error:

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

I am trying those steps:

  1. Go to https://console.cloud.google.com
  2. Create a project (or select existing project)
  3. Create a service account with role Pub/Sub Admin
  4. Go to https://console.cloud.google.com/apis/library and search for “Google Play Android Developer API”
  5. Enable that API
  6. Go to https://play.google.com/apps/publish
  7. Go to Settings > Developer account > API Access
  8. Link the project that you created in step 2
  9. The service account will appear that you created in step 3
  10. Grant access to it with Finance permission to the app in play console
  11. Wait for at least 24 hours for changes to take effect.

I am trying this code :

        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());
                }
            }

I am using testing In-App Purchase mode.

I want to fetch the latest subscription receipt after renewing subscription plan.

Advertisement

Answer

I have checked after 24 hours there was an error. But after that, I have checked again after two days there was no error and the data has been received.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement