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

Ren*_*ldo 7 android in-app-purchase service-accounts google-play-developer-api google-play-console

我按照本教程: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."
}
Run Code Online (Sandbox Code Playgroud)

这是我的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());
Run Code Online (Sandbox Code Playgroud)

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

Dmi*_*ich 5

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

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

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

其中jsonCertJSON 密钥(服务帐户 -> 选择帐户 -> 创建密钥)看起来像

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

Run Code Online (Sandbox Code Playgroud)

更新:根据 @Q Locker,Google 在创建后可能需要一些时间来配置服务帐户(@Q Locker 的情况最多需要 2 天)。