我终于可以在 Google Play 上查看我的购买记录了。当我通过移动应用购买商品时,Google Play 会向我发送收据。然后,我将此收据发送到我的服务器,我只需要在服务器端做两件事:
- 使用之前生成的服务帐号 Json 来获取我的 google 凭据
- 使用 google 凭据和收据从 google play 获取购买信息。
我使用这两个函数来执行它:
private static GoogleCredential getGoogleCredential() throws IOException {
List<String> scopes = new ArrayList<String>();
scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);
ClassLoader classLoader = MY_CLASS.class.getClassLoader();
GoogleCredential credential = GoogleCredential.fromStream(classLoader.getResourceAsStream(GOOGLE_KEY_FILE_PATH))
.createScoped(scopes);
return credential;
}
private static ProductPurchase getPurchase(GoogleReceipt receipt, GoogleCredential credential)
throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(YOUR_APPLICATION_NAME).build();
AndroidPublisher.Purchases purchases = publisher.purchases();
final Get request = purchases.products().get(receipt.getPackageName(), receipt.getProductId(),
receipt.getPurchaseToken());
final ProductPurchase purchase = request.execute();
return purchase;
}
使用购买对象,我可以在一分钟内验证购买。
编辑:哦,不要在 API 包上查找 GoogleReceipt 类。这只是我为解析收据信息而创建的一个基本类。