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

问题描述 投票:7回答:2

我按照本教程: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());

我创建了一个服务帐户并授予了所有者角色。我去了谷歌播放控制台,在用户和权限我也让这个服务帐户作为管理员。

android in-app-purchase service-accounts google-play-console google-play-developer-api
2个回答
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": "[email protected]",
"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天)。


2
投票

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

服务帐户创建不会立即生效,在我的情况下,我等了不到2天才能使其正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.