如何验证应用内购买的Android服务器端Java?

时间:2018-10-16 23:26:04

标签: android in-app-purchase service-accounts google-play-console google-play-developer-api

我遵循了本教程:https://medium.com/@infinitesimal_/doing-android-purchase-validation-api-v3-in-java-5c46fc837368

但是我无法使其正常工作!我所能得到的就是这个:

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

这是我的Java代码:

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();    

    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();        
    String applicationName = "My App";       
    String packageName = "com.my.package";
    final Set<String> scopes = Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER);
    GoogleCredential credential = new GoogleCredential.Builder()                    
        .setTransport(httpTransport)                        
        .setJsonFactory(jsonFactory)                    
        .setServiceAccountId("myAccoutId")                     
        .setServiceAccountScopes(scopes)                    
        .setServiceAccountPrivateKeyFromP12File(
          new File("/my/path"))                    
        .build();

    AndroidPublisher pub = new AndroidPublisher.Builder
            (httpTransport, jsonFactory, credential)                     
            .setApplicationName(applicationName)                    
            .build();            
        final AndroidPublisher.Purchases.Products.Get get = 
            pub.purchases()
            .products()                    
            .get(packageName, "name", "transactionId"); 
        final ProductPurchase purchase = get.execute();            
        System.out.println("Found google purchase item " + purchase.toPrettyString());

我创建了一个服务帐户,并授予了所有者角色。我转到了Google Play控制台,在“用户和权限”中,我还将该服务帐户设置为管理员。

2 个答案:

答案 0 :(得分:2)

我们使用了相同的示例,并进行了大量更改以使其正常工作:

  1. 使用程序包名称代替应用程序名称:将.setApplicationName(applicationName)替换为.setApplicationName(packageName)

  2. 我们使用JSON密钥代替P12: GoogleCredential credential = GoogleCredential.fromStream(IOUtils.toInputStream(jsonCert));

其中jsonCert是一个JSON密钥(服务帐户->选择一个帐户->创建密钥)外观

{
"type": "service_account",
"project_id": "api-xxx",
"private_key_id": "xxx",
"private_key": "your_private_key",
"client_email": "api-server-service-account@api-xxx.iam.gserviceaccount.com",
"client_id": "xxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/api-server-service-account%40api-xxx.iam.gserviceaccount.com"
};

更新:根据@Q Locker,Google创建服务帐户后可能会花费一些时间(@Q Locker最多需要2天)。

答案 1 :(得分:2)

德米特里·博格达诺维奇的回答没有帮助。

创建服务帐户并不会立即生效,就我而言,我等待了不到2天才能使其生效。

相关问题