php - 如何在 PHP 中验证 Google Cloud Services 中的服务帐户?

标签 php google-cloud-platform recaptcha

在开发 Recaptcha Enterprise 以使用 V2“我不是机器人”复选框时,我遇到了这个错误:

Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information

我点击链接并确定了这个来进行身份验证:

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
  'keyFile' => json_decode(file_get_contents($path_to_keyfile), true),
  'projectId' => 'MY_PROJECT'
]);

我找不到任何其他建议我需要做任何更多的事情,并且构造函数 API 的 this link 不建议我可以将它作为参数传递然后继续。我不想为这个项目使用环境变量,我想在代码中手动连接。我错过了什么?我可以确认我有一个有效的服务帐户。

如果它有帮助,我想在我大概进行身份验证后尝试运行的代码是这样的:

// ==================== CAPTCHA ===================
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\Event;
use Google\Cloud\RecaptchaEnterprise\V1\Assessment;
use Google\Cloud\RecaptchaEnterprise\V1\TokenProperties\InvalidReason;

$captcha_response = $_POST['g-recaptcha-response'];
$site_key = "123456789abc";

$client = new RecaptchaEnterpriseServiceClient();

define('SITE_KEY', $site_key);
define('TOKEN', $captcha_response);
define('PROTECTED_ACTION', 'signup');
define('PARENT_PROJECT', 'projects/MY_PROJECT');

$event = (new Event())
     ->setSiteKey(SITE_KEY)
     ->setExpectedAction(PROTECTED_ACTION)
     ->setToken(TOKEN);

 $assessment = (new Assessment())
     ->setEvent($event);

 try {
     $response = $client->createAssessment(
         PARENT_PROJECT,
         $assessment
     );

     if ($response->getTokenProperties()->getValid() == false) {
         printf('The CreateAssessment() call failed because the token was invalid for the following reason: ');
         printf(InvalidReason::name($response->getTokenProperties()->getInvalidReason()));
     } else {
         if ($response->getEvent()->getExpectedAction() == PROTECTED_ACTION) {
             printf('The score for the protection action is:');
             printf($response->getRiskAnalysis()->getScore());
         }
         else
         {
             printf('The action attribute in your reCAPTCHA tag does not match the action you are expecting to score');
         }
     }
 } catch (exception $e) {
     printf('CreateAssessment() call failed with the following error: ');
     printf($e);
 }

最佳答案

这是我如何让它工作的。感谢 John Hanley 在之前的回答中提供的帮助。文档让我相信(无论出于何种原因)存储是必需的,但事实并非如此:它就像通过 credentials 参数提供 key 路径一样简单。 不是 keyFile 参数。

if (empty($_POST['g-recaptcha-response']))
die("You have failed the not-a-robot check.");

$captcha_response = $_POST['g-recaptcha-response'];

require 'composer/vendor/autoload.php';

use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\Event;
use Google\Cloud\RecaptchaEnterprise\V1\Assessment;
use Google\Cloud\RecaptchaEnterprise\V1\TokenProperties\InvalidReason;

$path_to_keyfile = "MY_PROJECT-1234567890abc.json";
$site_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$client = new RecaptchaEnterpriseServiceClient([
  'credentials' => $path_to_keyfile,
  'projectId' => 'MY_PROJECT'
]);

define('SITE_KEY', $site_key);
define('TOKEN', $captcha_response);
define('PROTECTED_ACTION', 'signup');
define('PARENT_PROJECT', 'projects/MY_PROJECT');

$event = (new Event())
     ->setSiteKey(SITE_KEY)
     ->setExpectedAction(PROTECTED_ACTION)
     ->setToken(TOKEN);

 $assessment = (new Assessment())
     ->setEvent($event);

 try {
     $response = $client->createAssessment(PARENT_PROJECT, $assessment);

     if ($response->getTokenProperties()->getValid() == false) {
         printf('The CreateAssessment() call failed because the token was invalid for the following reason: ');
         printf(InvalidReason::name($response->getTokenProperties()->getInvalidReason()));
         exit;
     } else {
         if ($response->getEvent()->getExpectedAction() == PROTECTED_ACTION) {
          // Closer to 1 = human, to 0 = robot.
          $bot_score = $response->getRiskAnalysis()->getScore();
          // do what you want with the score here...

         } else {
             die('The action attribute in your reCAPTCHA tag does not match the action you are expecting to score');
         }
     }
 } catch (exception $e) {
     printf('CreateAssessment() call failed with the following error: ');
     printf($e);
     exit;
 }

关于php - 如何在 PHP 中验证 Google Cloud Services 中的服务帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68279157/

相关文章:

php - 为什么 var_dump 会返回比字符串长度更大的值?

php - Facebook Jquery ajax 权限请求?

python - 访问 python 类实例变量时出错

javax.net.ssl.SSLHandshakeException : sun. security.validator.ValidatorException:PKIX 路径构建失败 google recaptcha

jquery - ReCaptcha 溢出/出屏

php - 如何修复 C : mail() [function. 中的警告 : SMTP server response: 530 SMTP authentication is required. mail] :\xampp\htdocs\. 。第 22 行错误

php - 如何计算PHP中JSON值的长度?

iOS 从用户文档 Firebase Firestore 存储/更新和检索位置数据

google-cloud-platform - 无法删除 google api 网关配置文件

java - 如何更改使用 Google ReCaptcha 版本 2 时的错误消息?