この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:7
ページ更新者:guest
更新日時:2019-10-08 18:05:13

タイトル: v3の使い方
SEOタイトル: reCAPTCHA v3 使い方(サンプル付き)

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<thead>
		<tr><th style="background-color:#f39c12;color:#ffffff;text-align:left">この記事の要点</th></tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#fef9e7">
			<ul>
				<li>reCAPTCHA v3 の<strong>使い方</strong>(v2 と異なり<strong>チェックボックス UI が出ない</strong>)</li>
				<li>前提: <strong>キー登録</strong>(<code>https://g.co/recaptcha/v3</code>)が必要</li>
				<li>クライアント: <strong><code><script src="https://www.google.com/recaptcha/api.js?render=サイトキー"></code></strong> を head に追加</li>
				<li>サーバ: <code>g-recaptcha-response</code> をサーバ検証 API へ POST し、<strong>スコア (0.0〜1.0)</strong> で bot 判定</li>
			</ul>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>本稿は<strong>reCAPTCHA v3 使い方</strong>について説明します。</p>

<p>&nbsp;</p>

<h2>前提</h2>

<p><strong>公式ドキュメント</strong>は以下のURLを参照。</p>

<p><a href="https://developers.google.com/recaptcha/intro">https://developers.google.com/recaptcha/intro</a></p>

<p>&nbsp;</p>

<p>reCAPTCHA v3を使用するには<span style="color:#e74c3c"><strong>キー登録</strong></span>が必要となる。</p>

<p>以下のURLからキー登録ができる。</p>

<p><a href="https://g.co/recaptcha/v3">https://g.co/recaptcha/v3</a></p>

<p>&nbsp;</p>

<h2>headタグ</h2>

<p>headタグ内に以下の記述をする。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">&lt;script src=&#39;https://www.google.com/recaptcha/api.js?render=</span><u><em><span style="color:#f1c40f">Site Key</span></em></u><span style="color:#ffffff">&#39;&gt;&lt;/script&gt;</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p><span style="color:#e74c3c"><strong>Site key</strong></span>は自身のものを入力する。</p>

<p>&nbsp;</p>

<h2>formの作成</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><br />
			<span style="color:#ffffff">&lt;form id=&quot;test_form&quot; method=&quot;POST&quot; action=&quot;testAction&quot;&gt;<br />
			<br />
			&nbsp; &nbsp; &lt;!-- 適当な入力値&nbsp;--&gt;<br />
			&nbsp; &nbsp; &lt;input id=&quot;user_name&quot; type=&quot;text&quot; name=&quot;user_name&quot;&nbsp;/&gt;</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &lt;!-- 送信ボタン --&gt;<br />
			&nbsp; &nbsp; &lt;input type=&quot;button&quot; value=&quot;送信&quot; onclick=&quot;</span><span style="color:#3498db">commentSubmitFunc()</span><span style="color:#ffffff">&quot;/&gt;</span></p>

			<p><span style="color:#ffffff">&lt;/form&gt;</span></p>

			<p><span style="color:#ffffff">&lt;!-- reCAPCTHA --&gt;</span></p>

			<p><span style="color:#ffffff">&lt;script&gt;</span></p>

			<p><span style="color:#ffffff">function </span><span style="color:#3498db">commentSubmitFunc()</span><span style="color:#ffffff"> {</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; grecaptcha.ready(function() {</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#e74c3c">grecaptcha.execute</span><span style="color:#ffffff">(&#39;</span><u><em><span style="color:#f1c40f">Site Key</span></em></u><span style="color:#ffffff">&#39;, {action: &#39;</span><span style="color:#2ecc71"><em>Action Name</em></span><span style="color:#ffffff">&#39;})</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; .then(function(</span><span style="color:#9b59b6">token</span><span style="color:#ffffff">) {</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(&#39;#test_form&#39;).prepend(&#39;&lt;input type=&quot;hidden&quot; name=&quot;</span><span style="color:#9b59b6">g-recaptcha-response</span><span style="color:#ffffff">&quot; value=&quot;&#39; + </span><span style="color:#9b59b6">token </span><span style="color:#ffffff">+ &#39;&quot;&gt;&#39;);</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(&#39;#test_form&#39;).submit();</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; });</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; });</span></p>

			<p><span style="color:#ffffff">}&lt;/script&gt;</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>formの送信ボタンを押したら<strong><span style="color:#e74c3c">grecaptcha.execute</span></strong>を呼び出すようにする。</p>

<p>その際に<strong><span style="color:#9b59b6">token</span></strong>をサーバーサイドに渡してやる必要がある。</p>

<p><strong><span style="color:#2ecc71"><em>Action Name</em></span></strong>には以下のものが指定できる。(公式サイトより抜粋)</p>

<blockquote>
<table cellspacing="0" style="border-collapse:collapse; border:none; width:701pt">
	<tbody>
		<tr>
			<td style="background-color:rgba(255, 255, 255, 0.95); height:18.0pt; vertical-align:bottom; white-space:normal; width:118pt"><strong><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">homepage</span></span></span></strong></td>
			<td style="height:18.0pt; vertical-align:bottom; white-space:nowrap; width:583pt"><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">See a cohesive view of your traffic on the admin console while filtering scrapers.</span></span></span></td>
		</tr>
		<tr>
			<td style="height:18.0pt; vertical-align:bottom; white-space:normal; width:118pt"><strong><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">login</span></span></span></strong></td>
			<td style="height:18.0pt; vertical-align:bottom; white-space:nowrap"><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">With low scores, require 2-factor-authentication or email verification to prevent credential stuffing attacks.</span></span></span></td>
		</tr>
		<tr>
			<td style="height:18.0pt; vertical-align:bottom; white-space:normal; width:118pt"><strong><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">social</span></span></span></strong></td>
			<td style="height:18.0pt; vertical-align:bottom; white-space:nowrap"><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">Limit unanswered friend requests from abusive users and send risky comments to moderation.</span></span></span></td>
		</tr>
		<tr>
			<td style="height:18.0pt; vertical-align:bottom; white-space:normal; width:118pt"><strong><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">e-commerce</span></span></span></strong></td>
			<td style="height:18.0pt; vertical-align:bottom; white-space:nowrap"><span style="font-size:11pt"><span style="color:black"><span style="font-family:&quot;Yu Gothic&quot;,monospace">Put your real sales ahead of bots and identify risky transactions.</span></span></span></td>
		</tr>
	</tbody>
</table>
</blockquote>

<p><span style="color:#e74c3c">※どなたか適切な日本語訳をお願いします。</span></p>

<p>本稿のサンプルでは<strong>homepage</strong>を指定する<strong>。</strong></p>

<p>&nbsp;</p>

<h2>サーバサイドの実装</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#9b59b6">$gRecaptchaResponse</span><span style="color:#ffffff"> = $_POST[&quot;</span><span style="color:#9b59b6">g-recaptcha-response</span><span style="color:#ffffff">&quot;];</span></p>

			<p><span style="color:#ffffff">$reCaptchaUrl = &quot;https://www.google.com/recaptcha/api/siteverify?secret=</span><span style="color:#3498db"><em>Secret key</em></span><span style="color:#ffffff">&amp;response=&quot; . </span><span style="color:#9b59b6">$gRecaptchaResponse</span><span style="color:#ffffff">;</span></p>

			<p><span style="color:#ffffff">$response = @file_get_contents($reCaptchaUrl);</span></p>

			<p><span style="color:#ffffff">$response = json_decode($response,true);</span></p>

			<p><span style="color:#ffffff">if ($response[&#39;</span><span style="color:#e74c3c"><strong>success</strong></span><span style="color:#ffffff">&#39;] != true or $response[&#39;</span><span style="color:#e74c3c"><strong>score</strong></span><span style="color:#ffffff">&#39;] &lt; 0.5 ){</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; //エラー処理</span></p>

			<p><span style="color:#ffffff">}</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p><strong><span style="color:#3498db"><em>Secret key</em></span></strong>は自身のものを入力する。</p>

<p>APIに<strong><span style="color:#3498db"><em>Secret key</em></span></strong>と<strong><span style="color:#9b59b6">token</span></strong>を渡すことでレスポンスを取得できる。</p>

<p>以下、レスポンスの内容。(公式サイトより抜粋)</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">{</span></p>

			<p><span style="color:#ffffff">&nbsp; &quot;<strong>success</strong>&quot;: true|false, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#f1c40f">// whether this request was a valid reCAPTCHA token for your site</span></p>

			<p><span style="color:#ffffff">&nbsp; &quot;<strong>score</strong>&quot;: number &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#f1c40f">// the score for this request (0.0 - 1.0)</span></p>

			<p><span style="color:#ffffff">&nbsp;&nbsp;&quot;action&quot;: string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#f1c40f">// the action name for this request (important to verify)</span></p>

			<p><span style="color:#ffffff">&nbsp;&nbsp;&quot;challenge_ts&quot;: timestamp, &nbsp;</span><span style="color:#f1c40f">// timestamp of the challenge load (ISO format yyyy-MM-dd&#39;T&#39;HH:mm:ssZZ)</span></p>

			<p><span style="color:#ffffff">&nbsp;&nbsp;&quot;hostname&quot;: string, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#f1c40f">// the hostname of the site where the reCAPTCHA was solved</span></p>

			<p><span style="color:#ffffff">&nbsp;&nbsp;&quot;error-codes&quot;: [...] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#f1c40f">// optional</span></p>

			<p><span style="color:#ffffff">}</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>ポイントは<span style="color:#e74c3c"><strong>success</strong></span><span style="color:null">と</span><span style="color:#e74c3c"><strong>score</strong></span><span style="color:null">の値。</span></p>

<p><span style="color:null"><strong>score</strong></span>の幅は1.0~0.0で低いほどbotである可能性が高くなる。</p>

<p><span style="color:#e74c3c"><strong>この値を見てbotかどうかを判断する必要がある。</strong></span></p>

<p><span style="color:#e74c3c"><strong>※どの程度の値を設定することがベストなのか情報求む。人が操作するとだいたい0.9になることが多いらしい。</strong></span></p>

<p>&nbsp;</p>