提问人:Anudeep Ananth 提问时间:9/14/2023 最后编辑:Anudeep Ananth 更新时间:9/15/2023 访问量:52
在 Ktor 服务器上验证 Google Play 购买
Validate Google Play Purchase at Ktor server
问:
我正在使用 Ktor 服务器来检查使用购买令牌在 google play 上进行的购买是否有效:
这是我的代码:
fun Route.validatePurchase() {
route("/validatePurchase") {
get {
val purchaseToken = call.request.queryParameters[PURCHASE_TOKEN_RF] ?: return@get
//create a tracker that contains the total items modified
val isValid = validateThePurchase(purchaseToken)
//Successful
if (isValid == "True") {
call.respond(HttpStatusCode.OK, isValid)
} else if ( isValid == "False") {
call.respond(HttpStatusCode.Forbidden, isValid)
} else if ( isValid == "Error") {
call.respond(HttpStatusCode.ServiceUnavailable, isValid) // This error is being returned always
}else {
call.respond(HttpStatusCode.NotImplemented, isValid)
}
}
}
}
private fun validateThePurchase(purchaseToken: String) : String {
val packageName = "com.packaganame.app"
val subscriptionId = "god_mode"
return try {
val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
val jsonFactory = JacksonFactory.getDefaultInstance()
val credentials = GoogleCredentials.getApplicationDefault()
.createScoped(listOf("https://androidpublisher.googleapis.com/auth/androidpublisher"))
val httpRequestInitializer = HttpCredentialsAdapter(credentials)
val androidPublisher = AndroidPublisher.Builder(httpTransport, jsonFactory, httpRequestInitializer)
.setApplicationName("com.package.app")
.build()
val subscriptionPurchase: SubscriptionPurchase = androidPublisher.Purchases()
.subscriptions()
.get(packageName, subscriptionId, purchaseToken)
.execute()
// Check if the subscription is currently active.
val currentTimeMillis = System.currentTimeMillis()
val expiryTimeMillis = subscriptionPurchase.expiryTimeMillis
if (currentTimeMillis < expiryTimeMillis) {
"True" // The subscription is active.
} else {
"False" // The subscription has expired.
}
} catch (e: Exception) {
//function enters this catch block
"Error"
}
}
我不断收到 HttpStatusCode.Forbidden 错误。
我还做了以下工作
- 在 Google Cloud Project 中启用了“Google Play Developer API”。
- 我已将默认的 Google App Engine 服务帐户与 Google Play 相关联
- 在 Google Play API 访问部分授予它“查看财务数据”和“管理订单/订阅”权限
关于我的代码有什么问题的任何建议?
答: 暂无答案
评论
isValid