使用PHP调用Google Wallet API创建通用卡类时触发INVALID_ARGUMENT错误
使用PHP调用Google Wallet API创建通用卡类时触发INVALID_ARGUMENT错误
我之前调用Google Wallet API时也碰到过类似的400错误,帮你分析下几个大概率的问题点,先从最显眼的错误开始排查:
1. URI格式错误(最可能的元凶)
我注意到你的imageModulesData和linksModuleData里的URI都写成了https=>//,这明显是输入/转义错误,正确格式应该是https://。Google Wallet API会严格校验URI格式,这种错误的URI会直接触发INVALID_ARGUMENT错误,这应该是你当前报错的主要原因。
2. 模板引用字段的合理性检查
你的卡片模板里引用了object.textModulesData["points"]和object.textModulesData["contacts"],但当前类定义里并没有这两个ID的文本模块。这里要说明:类模板里的fieldPath是引用**Pass(对象)**里的字段,而非类本身的,所以如果后续你的Pass对象会添加这两个文本模块,这个引用是没问题的;但如果Pass里也没定义,后续创建Pass时可能会报错,但这不是当前创建类时报错的原因。
修正后的类定义示例
把URI错误修正后,你的类定义应该是这样的:
$classDef = [ 'id' => $issuerId . '.' . self::CLASS_SUFFIX, 'classTemplateInfo' => [ 'cardTemplateOverride' => [ 'cardRowTemplateInfos' => [ [ 'twoItems' => [ 'startItem' => [ 'firstValue' => [ 'fields' => [ [ 'fieldPath' => 'object.textModulesData["points"]' ] ] ] ], 'endItem' => [ 'firstValue' => [ 'fields' => [ [ 'fieldPath' => 'object.textModulesData["contacts"]' ] ] ] ] ] ] ] ], 'detailsTemplateOverride' => [ 'detailsItemInfos' => [ [ 'item' => [ 'firstValue' => [ 'fields' => [ [ 'fieldPath' => 'class.imageModulesData["event_banner"]' ] ] ] ] ], [ 'item' => [ 'firstValue' => [ 'fields' => [ [ 'fieldPath' => 'class.textModulesData["game_overview"]' ] ] ] ] ], [ 'item' => [ 'firstValue' => [ 'fields' => [ [ 'fieldPath' => 'class.linksModuleData.uris["official_site"]' ] ] ] ] ] ] ] ], 'imageModulesData' => [ [ 'mainImage' => [ 'sourceUri' => [ 'uri' => 'https://storage.googleapis.com/wallet-lab-tools-codelab-artifacts-public/google-io-2021-card.png' ], 'contentDescription' => [ 'defaultValue' => [ 'language' => 'en-US', 'value' => 'Google I/O 2022 Banner' ] ] ], 'id' => 'event_banner' ] ], 'textModulesData' => [ [ 'header' => 'Gather points meeting new people at Google I/O', 'body' => 'Join the game and accumulate points in this badge by meeting other attendees in the event.', 'id' => 'game_overview' ] ], 'linksModuleData' => [ 'uris' => [ [ 'uri' => 'https://io.google/2022/', 'description' => 'Official I/O \'22 Site', 'id' => 'official_site' ] ] ] ];
额外排查建议
如果修正URI后还是报错,你可以把PHP数组转成JSON字符串(用json_encode($classDef)),然后用Google Wallet API的官方校验逻辑去验证请求体是否符合规范,能得到更具体的错误提示,帮你定位剩余问题。
备注:内容来源于stack exchange,提问作者Nir ina
相关产品推荐
相关产品推荐

