Google 分析 API 请求 returns 验证码?

Google Analytics API request returns captcha?

从昨天开始,我们连接到 Google Analytics API returns 一个 HTML 页面,带有...验证码。有人遇到过这样的事情吗?

我们将 API 与 PHP 库一起使用。我们检索报告的代码(包装在我们自己的 class 中,但它使用 google/apiclient 包):

// Create analytics
$this->analytics = new \Google_Service_AnalyticsReporting($this->client);

// Create the ReportRequest object.
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($view_id);
$request->setDateRanges($this->dateRanges);
$request->setMetrics( array_values( $this->metrics ) );
$request->setDimensions( array_values( $this->dimensions ) );
$request->setSegments($this->segments);

$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );

try {
    $reports = $this->analytics->reports->batchGet( $body );
}
catch(\Google_Service_Exception $e)
{
    echo"Error (1)...\n";
    echo $e->getMessage() . "\n";

    $this->lastError = json_decode($e->getMessage());
    return false;
}

我们捕获了一个Google Service Exception,错误信息如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta name="viewport" content="initial-scale=1"><title>https://analyticsreporting.googleapis.com/v4/reports:batchGet</title></head>
<body style="font-family: arial, sans-serif; background-color: #fff; color: #000; padding:20px; font-size:18px;" onload="e=document.getElementById('captcha');if(e){e.focus();}">
<div style="max-width:400px;">
<hr noshade size="1" style="color:#ccc; background-color:#ccc;"><br>
<form id="captcha-form" action="index" method="post">
To continue, please type the characters below:<br><br>
<img src="/sorry/image?id=14645528721876966739&amp;q=EgSIkL9bGIjOzu4FIhkA8aeDSycUmKeaKy3bPExiN-Ol2PpxQzOfMgFj&amp;hl=en&amp;continue=https://analyticsreporting.googleapis.com/v4/reports:batchGet" border="1" alt="Please enable images"><br><br>
<input type="text" name="captcha" value="" id="captcha" size="12" style="font-size:16px; padding:3px 0 3px 5px; margin-left:0px;"><br><br><br>
<input type="submit" name="btn-submit" value="Submit" style="font-size:18px; padding:4px 0;">
<input type='hidden' name='q' value='EgSIkL9bGIjOzu4FIhkA8aeDSycUmKeaKy3bPExiN-Ol2PpxQzOfMgFj'><input type="hidden" name="continue" value="https://analyticsreporting.googleapis.com/v4/reports:batchGet">
</form>
<hr noshade size="1" style="color:#ccc; background-color:#ccc;">

<div style="font-size:13px;">
<b>About this page</b><br><br>

Our systems have detected unusual traffic from your computer network.  This page checks to see if it&#39;s really you sending the requests, and not a robot.  <a href="#" onclick="document.getElementById('infoDiv').style.display='block';">Why did this happen?</a><br><br>

<div id="infoDiv" style="display:none; background-color:#eee; padding:10px; margin:0 0 15px 0; line-height:1.4em;">
This page appears when Google automatically detects requests coming from your computer network which appear to be in violation of the <a href="//www.google.com/policies/terms/">Terms of Service</a>. The block will expire shortly after those requests stop.  In the meantime, solving the above CAPTCHA will let you continue to use our services.<br><br>This traffic may have been sent by malicious software, a browser plug-in, or a script that sends automated requests.  If you share your network connection, ask your administrator for help &mdash; a different computer using the same IP address may be responsible.  <a href="//support.google.com/websearch/answer/86640">Learn more</a><br><br>Sometimes you may be asked to solve the CAPTCHA if you are using advanced terms that robots are known to use, or sending requests very quickly.
</div>

IP address: xxx.xxx.xxx.xxx<br>Time: 2019-11-19T08:25:44Z<br>URL: https://analyticsreporting.googleapis.com/v4/reports:batchGet<br>
</div>
</div>
</body>
</html>

如果有人也遇到这个问题或有解决办法,请告诉我。

提前致谢。

这可能是 Google 应用一些安全措施来应对流量激增。我还知道他们在 2019 年 11 月 4 日更新了 API 的错误消息,这可能就是为什么我们现在收到此错误而不是之前的其他错误的原因。

我从 Google 收到的电子邮件:

"we’re upgrading the Google Analytics APIs to a new technical infrastructure stack beginning November 4, 2019. As a result of this change, clients will see some differences in the response error message text for failed API requests."

"If your application or log analysis workflow rely on the specific wording provided in responses of the Management API v3 or Core Reporting API v3, you will need to update the code to reflect the new error response text. Please note that as exact error message wording can change at any time, we strongly discourage hardcoding the specific message text when processing responses from the API."

今天我的请求失败了,我的错误日志中提供了一个生成 404 的验证码图像。 我收到的错误:

"Our systems have detected unusual traffic from your computer network. This page checks to see if it's really you sending the requests, and not a robot."

"The block will expire shortly after those requests stop. In the meantime, solving the above CAPTCHA will let you continue to use our services."

此后我发现该服务可以少量恢复 运行,无需任何代码更改。

我建议重试并限制请求数量,直到服务恢复 100%。