调用Recaptcha Enterprise遇hashed_account_id弃用错误求助
解决Recaptcha Enterprise中hashed_account_id弃用错误
核心解决步骤
- 升级
google/cloud-recaptcha-enterpriseSDK至最新稳定版本,确保使用支持account_id字段的版本,彻底脱离已弃用的hashed_account_id逻辑。 - 对用户账号ID进行SHA-256哈希处理(转为小写十六进制格式)后,再传入
UserInfo对象的setAccountId方法,这是当前Recaptcha Enterprise要求的规范。
修改后的代码
public function createAssessment(string $token, string $platform, string $ID = ''): Assessment { $siteKey = $this->determineSiteKey($platform); $projectName = $this->client->projectName($this->settings->project); $userInfo = new UserInfo(); // 仅当ID非空时,生成哈希并设置 if (!empty($ID)) { // 统一格式:转小写+去空格后哈希 $hashedAccountId = hash('sha256', strtolower(trim($ID))); $userInfo->setAccountId($hashedAccountId); } $event = (new Event())->setSiteKey($siteKey)->setToken($token)->setUserInfo($userInfo); $assessment = (new Assessment())->setEvent($event); try { $assessment = $this->client->createAssessment($projectName, $assessment); } catch (\Exception $e) { throw new Exception($e->getMessage()); } return $assessment; } /** * Determines the appropriate site key based on the platform. * * @param string $platform The platform (e.g., iOS, Android). * @return string The site key for the platform. */ private function determineSiteKey(string $platform): string { return $platform === 'IOS' ? $this->settings->siteKey_IOS : $this->settings->siteKey_android; }
关键说明
- 哈希处理时统一对ID做小写和去空格处理,保证不同场景下用户ID的一致性,避免影响Recaptcha的评估准确性。
- 升级SDK后,旧的
hashed_account_id字段相关的弃用提示会消失,因为新版本已完全使用account_id替代。
内容的提问来源于stack exchange,提问作者ali heydarabadii
相关产品推荐
相关产品推荐

