ข้ามไปยังเนื้อหา
สถานะแดชบอร์ด

คู่มือ

ส่ง token ของ reCAPTCHA

หน้านี้แปลด้วยเครื่องจากเวอร์ชันภาษาจีน อาจมีข้อผิดพลาด โปรดยึดเวอร์ชันภาษาจีนเป็นหลัก

gRecaptchaResponse ที่ YesCaptcha คืนให้ คือผลลัพธ์เดียวกับที่ Google ส่งให้เว็บเพจหลังจากที่คุณทำ CAPTCHA บนหน้าเว็บเสร็จ เมื่อได้รับแล้ว คุณต้องส่งให้เว็บไซต์เป้าหมายเหมือนที่เบราว์เซอร์ทำ (ไม่ใช่ส่งให้ Google หรือ YesCaptcha) เว็บไซต์จะนำไปตรวจสอบกับ Google แล้วจึงอนุญาตให้ผ่าน มีสองวิธี คือ ส่งคำขอโดยตรง หรือกรอกลงในหน้าเว็บในเบราว์เซอร์แล้วส่ง

ขั้นตอนเมื่อเข้าใช้งานตามปกติ:

  1. เว็บเพจโหลด reCAPTCHA และผู้ใช้ทำการยืนยันให้เสร็จ;
  2. Google ส่ง token ให้เว็บเพจ และเว็บเพจใส่ไว้ในช่องข้อความ g-recaptcha-response (หรือส่งให้ฟังก์ชัน callback);
  3. ผู้ใช้ส่งฟอร์ม และ token ถูกส่งไปยังเซิร์ฟเวอร์ของเว็บไซต์พร้อมกับฟอร์ม;
  4. เซิร์ฟเวอร์ของเว็บไซต์นำ token ไปตรวจสอบกับ Google ถ้าผ่านก็อนุญาตให้เข้า

เมื่อใช้ YesCaptcha เราจะทำขั้นตอนที่ 1 และ 2 ให้ คุณสร้างงานด้วย websiteURL และ websiteKey รับ token แล้วทำต่อจากขั้นตอนที่ 3

  1. ลองทำด้วยมือหนึ่งครั้งในเบราว์เซอร์: กด F12 เพื่อเปิดแผง Network ทำ CAPTCHA ให้เสร็จแล้วส่งฟอร์ม จากนั้นหาคำขอที่ใช้ส่งฟอร์ม
  2. ใน Payload (หรือ Form Data) ของคำขอ ให้หาฟิลด์ที่มี token ซึ่งมักชื่อ g-recaptcha-response และค่าของ token ขึ้นต้นด้วย 03A บางเว็บไซต์เปลี่ยนชื่อฟิลด์ เช่น grecaptcharesponse หรือ captcha
  3. ส่งคำขอแบบเดียวกันจากโปรแกรมของคุณ โดยเปลี่ยนค่าของฟิลด์นั้นเป็น token ที่ YesCaptcha คืนให้ ฟิลด์อื่น, Cookie และ header ต้องตรงกับที่เบราว์เซอร์ส่งด้วย เพราะเว็บไซต์มักตรวจสิ่งเหล่านี้พร้อมกัน

ตัวอย่างฉบับเต็มบนหน้าเดโมของ Google: ขอ token ก่อน แล้วส่ง token กลับไปยังหน้าเดโมด้วยฟิลด์ g-recaptcha-response:

recaptcha_requests.py
"""Solve reCAPTCHA v2 on Google's demo page and submit the token with plain HTTP (no browser).
Run: pip install requests
export YESCAPTCHA_CLIENT_KEY=your_client_key
python recaptcha_requests.py
Each run creates one NoCaptchaTaskProxyless task (charged per the price list).
"""
import os
import time
import requests
CLIENT_KEY = os.environ["YESCAPTCHA_CLIENT_KEY"]
API = "https://api.yescaptcha.com"
WEBSITE_URL = "https://www.google.com/recaptcha/api2/demo"
WEBSITE_KEY = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"
def call(method, payload):
data = requests.post(f"{API}/{method}", json={"clientKey": CLIENT_KEY, **payload}, timeout=30).json()
if data.get("errorId"):
raise RuntimeError(f"{data.get('errorCode')}: {data.get('errorDescription')}")
return data
def get_token():
task = {"type": "NoCaptchaTaskProxyless", "websiteURL": WEBSITE_URL, "websiteKey": WEBSITE_KEY}
task_id = call("createTask", {"task": task})["taskId"]
for _ in range(36): # poll every 5 seconds, for up to about 3 minutes
time.sleep(5)
result = call("getTaskResult", {"taskId": task_id})
if result.get("status") == "ready":
return result["solution"]["gRecaptchaResponse"]
raise TimeoutError(task_id)
token = get_token()
print("token:", token[:40], "...")
# Submit the token the same way the browser does: the demo form posts the field
# "g-recaptcha-response" back to the page. Check your own site's request in DevTools > Network.
page = requests.post(WEBSITE_URL, data={"g-recaptcha-response": token}, timeout=30)
print("passed" if "Verification Success" in page.text else "not passed", page.status_code)

วิธีที่ 2: กรอกในเบราว์เซอร์ (Selenium และเครื่องมืออื่น)

หัวข้อที่มีชื่อว่า “วิธีที่ 2: กรอกในเบราว์เซอร์ (Selenium และเครื่องมืออื่น)”

เมื่อควบคุมเบราว์เซอร์จริงด้วยเครื่องมือเช่น Selenium หรือ Playwright:

  1. อ่านค่า data-sitekey จากหน้าเว็บ แล้วใช้ค่านั้นสร้างงานเพื่อรับ token
  2. เขียน token ลงในช่องข้อความ g-recaptcha-response บนหน้าเว็บ
  3. ถ้าหน้าเว็บกำหนดฟังก์ชัน callback ไว้ (data-callback="...") ให้เรียกฟังก์ชันนั้นพร้อมส่ง token บางเว็บไซต์ปุ่มส่งจะกดได้ก็ต่อเมื่อเรียก callback แล้วเท่านั้น
  4. ส่งฟอร์ม
// Step 2: write the token (run in the page)
document.getElementById('g-recaptcha-response').value = 'TOKEN';
// Step 3: call the callback if there is one, e.g. data-callback="onSuccess"
onSuccess('TOKEN');

ถ้าหน้าเว็บมี reCAPTCHA หลายตัว id ของช่องข้อความอาจเป็นแบบ g-recaptcha-response-1 หากหาช่องข้อความนี้ไม่เจอ ให้ทำการยืนยันด้วยมือหนึ่งครั้ง แล้วค้นหา 03A ใน Elements เพื่อดูว่าผลลัพธ์ถูกเก็บไว้ที่ใด วิธีหาฟังก์ชัน callback ดูได้ที่ ค้นหาพารามิเตอร์ reCAPTCHA

ตัวอย่างฉบับเต็ม (Selenium 4):

recaptcha_selenium.py
"""Solve reCAPTCHA v2 on Google's demo page in a real browser with Selenium 4:
get a token from YesCaptcha, put it into the page, then submit the form.
Run: pip install requests selenium
export YESCAPTCHA_CLIENT_KEY=your_client_key
python recaptcha_selenium.py
Each run creates one NoCaptchaTaskProxyless task (charged per the price list).
"""
import os
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
CLIENT_KEY = os.environ["YESCAPTCHA_CLIENT_KEY"]
API = "https://api.yescaptcha.com"
WEBSITE_URL = "https://www.google.com/recaptcha/api2/demo"
def call(method, payload):
data = requests.post(f"{API}/{method}", json={"clientKey": CLIENT_KEY, **payload}, timeout=30).json()
if data.get("errorId"):
raise RuntimeError(f"{data.get('errorCode')}: {data.get('errorDescription')}")
return data
def get_token(website_url, website_key):
task = {"type": "NoCaptchaTaskProxyless", "websiteURL": website_url, "websiteKey": website_key}
task_id = call("createTask", {"task": task})["taskId"]
for _ in range(36):
time.sleep(5)
result = call("getTaskResult", {"taskId": task_id})
if result.get("status") == "ready":
return result["solution"]["gRecaptchaResponse"]
raise TimeoutError(task_id)
driver = webdriver.Chrome() # Selenium 4 downloads a matching chromedriver automatically
try:
driver.get(WEBSITE_URL)
site_key = driver.find_element(By.CSS_SELECTOR, "[data-sitekey]").get_attribute("data-sitekey")
token = get_token(WEBSITE_URL, site_key)
# Put the token where reCAPTCHA keeps it ...
driver.execute_script(
"document.getElementById('g-recaptcha-response').value = arguments[0];", token
)
# ... and, if the page uses a callback (data-callback="..."), call it:
callback = driver.find_element(By.CSS_SELECTOR, "[data-sitekey]").get_attribute("data-callback")
if callback:
driver.execute_script(f"window[{callback!r}](arguments[0]);", token)
driver.find_element(By.ID, "recaptcha-demo-submit").click()
time.sleep(2)
print("passed" if "Verification Success" in driver.page_source else "not passed")
finally:
driver.quit()

v3 ไม่มีช่องติ๊ก เว็บเพจจะเรียก grecaptcha.execute(SITE_KEY, {action: '...'}) เมื่อต้องการ token แล้วใส่ token ลงในคำขอของตัวเอง (ชื่อฟิลด์ขึ้นอยู่กับเว็บไซต์ ที่พบบ่อยคือ g-recaptcha-response, token และ recaptchaToken) วิธีทำเหมือนวิธีที่ 1 คือหาคำขอที่มี token ในแผง Network แล้วส่งตามแบบนั้น และตอนสร้างงาน pageAction ต้องตรงกับ action ของหน้าเว็บ

  • token หมดอายุ: ส่งโดยเร็วหลังได้รับ อย่าเกิน 60 วินาที
  • ส่งผิดฟิลด์ที่เว็บไซต์ตรวจสอบ หรือขาดฟิลด์อื่นหรือ Cookie: เทียบกับคำขอในเบราว์เซอร์ทีละรายการ
  • เว็บไซต์ตรวจสอบ IP, User-Agent ฯลฯ พร้อมกัน: ใช้ประเภทงานที่มีพร็อกซีในตัว (เช่น NoCaptchaTask) และส่งผ่านพร็อกซีตัวเดียวกัน
  • คะแนน (score) ของ v3 ไม่พอ: ตรวจสอบ pageAction หรือเปลี่ยนไปใช้ประเภทงานที่ให้คะแนนสูงคงที่ ดู reCAPTCHA v3
  • ระบบควบคุมความเสี่ยงของเว็บไซต์ตรวจสัญญาณพฤติกรรมอื่นด้วย ถ้า token ผ่านน้อย ให้เปลี่ยนไปใช้ การจดจำภาพ แล้วคลิกจริงในเบราว์เซอร์ หรือใช้ ส่วนขยายเบราว์เซอร์