我正在尝试将应用程序检查用于我的云功能。按照文档操作后,我仍然无法使其正常工作。这是我从应用程序初始化应用程序检查的代码、函数代码本身以及调用函数的代码。最后,我将添加我所做的其他非代码事情的屏幕截图
初始化应用程序检查(在主活动中)
FirebaseApp.initializeApp(this)
Firebase.appCheck.installAppCheckProviderFactory(
PlayIntegrityAppCheckProviderFactory.getInstance(),
)
在 logcat 中我发现了这个:
Error getting App Check token; using placeholder token instead. Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: App attestation failed.
Python云运行函数代码 我假设我需要做的就是添加强制应用程序检查行。
from firebase_functions import https_fn
from google.auth import default
from googleapiclient.discovery import build
from firebase_admin import initialize_app
# Initialize the Firebase Admin SDK
initialize_app()
@https_fn.on_call(
enforce_app_check = True
)
def verify_purchase(req: https_fn.CallableRequest) -> dict:
"""Callable Cloud Function to verify Android subscription purchases."""
print("Request: ", req)
try:
# Use Application Default Credentials (ADC)
credentials, _ = default(scopes=['https://www.googleapis.com/auth/androidpublisher'])
androidpublisher = build('androidpublisher', 'v3', credentials=credentials)
except Exception as e:
raise https_fn.HttpsError(
code=https_fn.FunctionsErrorCode.INTERNAL,
message="Failed to initialize credentials or service",
details=str(e)
)
# Extracting request data
purchase_token = req.data.get('token')
print("Purchase Token: ", purchase_token)
subscription_id = req.data.get('subscription_id')
print("Subscription ID: ", subscription_id)
package_name = "" #Ive added this in my real code
if not purchase_token:
raise https_fn.HttpsError(
code=https_fn.FunctionsErrorCode.INVALID_ARGUMENT,
message="Token is required"
)
if not subscription_id:
raise https_fn.HttpsError(
code=https_fn.FunctionsErrorCode.INVALID_ARGUMENT,
message="Subscription ID is required"
)
try:
subscription = androidpublisher.purchases().subscriptions().get(
packageName=package_name,
subscriptionId=subscription_id,
token=purchase_token
).execute()
return {"data": subscription}
except Exception as e:
raise https_fn.HttpsError(
code=https_fn.FunctionsErrorCode.UNKNOWN,
message="Error verifying subscription",
details=str(e)
)
从App调用函数
private fun verifyPurchaseWithFirebase(purchaseToken: String, subscriptionId: String) {
val data = hashMapOf(
"token" to purchaseToken,
"subscription_id" to subscriptionId
)
Log.d(TAG, "Verifying purchase with token: $purchaseToken")
Log.d(TAG, "Data to send to Firebase: $data")
functions
.getHttpsCallable("verify_purchase")
.call(data)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(TAG, "Purchase verified successfully with Firebase")
val result = task.result?.data as? Map<String, Any>
if (result != null) {
updateUserSubscriptionStatus(result)
} else {
Log.e(TAG, "No data received from Firebase function")
}
} else {
Log.e(TAG, "Failed to verify purchase with Firebase", task.exception)
}
}
}
使用 256 SHA 指纹在 Firebase 上设置应用程序检查。 认证提供商:Play Integrity,状态:已注册 启用 Play Integrity API,错误率为 100%。 启用 Play Integrity API 我觉得奇怪的一件事......在我的游戏控制台上,它说我的集成才刚刚开始,尚未完成。我的云项目也已经链接了,我尝试过解除链接和重新链接都没有用,也等了半天。 集成 Integrity API 已启动
如果我遗漏了什么,请告诉我,谢谢。