You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SubscriptionV2 API返回Invalid Value的问题排查与解决

Android Publisher Subscriptionsv2接口400错误:Invalid Value 原因及解决方法

问题背景

使用Adapty提供的格式类似GPA.3376-9095-7476-99xxx的交易ID作为purchase token,调用Google Android Publisher的subscriptionsv2接口时,返回如下400错误:

<HttpError 400 when requesting https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.comapny.app/purchases/subscriptionsv2/tokens/GPA.3376-9095-7476-99xxx?alt=json returned "Invalid Value". Details: "[{'message': 'Invalid Value', 'domain': 'global', 'reason': 'invalid'}]">

调用代码如下:

def check_android_subscription(purchase_token, logger):
    credentials = service_account.Credentials.from_service_account_file(
        settings.GOOGLE_API_CREDENTIALS
    )

    response = (
        googleapiclient.discovery.build(
            "androidpublisher", "v3", credentials=credentials
        )
        .purchases()
        .subscriptionsv2()
        .get(packageName="com.comapny.app", token=purchase_token)
        .execute()
    )

    logger(f"Response: {response}", level="INFO")

    subscription_state = response.get('subscriptionState', SUBSCRIPTION_STATE_UNSPECIFIED)
    is_pro = subscription_state in [SUBSCRIPTION_STATE_ACTIVE, SUBSCRIPTION_STATE_IN_GRACE_PERIOD]

    ProUserProfile.objects.update_or_create(
        id=purchase_token,
        defaults={'is_pro': is_pro}
    )

    return is_pro

原因分析

  • 参数类型错误:Adapty返回的GPA.xxx格式字符串是Google Play的订单号(orderId),而非接口要求的purchase token。两者是完全不同的字段:
    • orderId是用户订阅的订单标识,格式固定为GPA.xxxx-xxxx-xxxx-xxxxx;
    • purchase token是Google Play生成的用于验证订阅有效性的唯一令牌,长度更长、无固定格式(通常是随机字符组合)。
  • 代码中调用subscriptionsv2().get()时,token参数必须传入purchase token,传入orderId会直接触发"Invalid Value"错误。

解决方法

  • 获取正确的purchase token:从Adapty的订阅数据中提取purchase_token字段(部分版本可能命名为google_purchase_token),将其作为参数传入接口,而非使用GPA开头的orderId。
  • 校验参数类型:调用接口前先确认传入的token是purchase token,避免混淆orderId和purchase token的用途。
  • 测试场景验证:如果是测试环境,确保使用Google Play测试账号生成的有效purchase token进行调用,不要仅用订单号替代。

内容的提问来源于stack exchange,提问作者Jenin Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 00:37:38