检查来自 Java 服务器的 Google Play 购买
Check a Google Play Purchase from Java Server
我正在尝试通过 Google 游戏 API 验证购买。我正在获取一个 purchaseId,我正在尝试通过调用 Purchases.products 来获取其状态。
我一直在按照这个问题的步骤进行操作 (),虽然了解我需要什么真的很棒,但我无法获得一个简单的工作示例 Java代码。
现在我有一个 JWT 令牌,我知道我需要用它来创建一个访问令牌(也许是创建一个 Credentials 对象?)才能调用 api,但是我'一直无法完成。
任何人都可以提供一个代码示例以了解如何做吗?
我终于可以通过 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 包裹上寻找 Google 收据 class。这只是我创建的用于解析收据信息的基本 class。
我正在尝试通过 Google 游戏 API 验证购买。我正在获取一个 purchaseId,我正在尝试通过调用 Purchases.products 来获取其状态。
我一直在按照这个问题的步骤进行操作 (
现在我有一个 JWT 令牌,我知道我需要用它来创建一个访问令牌(也许是创建一个 Credentials 对象?)才能调用 api,但是我'一直无法完成。
任何人都可以提供一个代码示例以了解如何做吗?
我终于可以通过 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 包裹上寻找 Google 收据 class。这只是我创建的用于解析收据信息的基本 class。