此页内容

HCaptcha协议接口

captchaRun

983字约3分钟

2024-05-21

HCaptcha

创建任务

参考 🔗创建任务: /v2/tasks

参数名类型是否必填说明
siteKeyStringHCaptcha SiteKey
siteRefererStringHCaptcha Referer
captchaTypeString固定为 HCaptcha
isInvisibleBool默认为false,隐形设为true,不隐形则为false
dataString

自定义数据,在某些 hCaptcha 实现中使用,主要与 isInvisible=true 配合使用。

数据字段的值可以从用于呈现验证码的 rqdata 参数中获取。

userAgentString仅传递来自 Windows 操作系统的实际 UA。
fallbackToActualUABool

true - 指定此参数时,我们将忽略用户在请求中发送的无关紧要的用户代理,并在 getTaskResult 中返回我们自己的(相关的)用户代理。这将提高令牌的接受率。

false - 我们插入请求中指定的用户代理。如果用户代理无效,您将收到错误。

cookiesString

在与目标页面交互期间必须使用的额外 cookies。

格式:cookiename1=cookievalue1; cookiename2=cookievalue2

请求样例

POST https://api.captcha.run/v2/tasks

Content-Type: application/json
Authorization: Bearer 00000000-0000-0000-0000-000000000000

{
  "captchaType": "HCaptcha",
  "siteKey": "00000000-0000-0000-0000-000000000000",
  "siteReferer": "https://www.hcaptcha.com/",
  "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
  "fallbackToActualUA": true
}

返回样例

{
  "taskId": "ece1508b-e44e-448b-8f28-cf835d05046b" //任务id
}

CURL 代码

curl -X POST "https://api.captcha.run/v2/tasks" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer 00000000-0000-0000-0000-000000000000" \
     -d '{
        "captchaType": "HCaptcha",
        "siteKey": "00000000-0000-0000-0000-000000000000",
        "siteReferer": "https://www.hcaptcha.com/",
        "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        "fallbackToActualUA": true
    }'

Python代码

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer 00000000-0000-0000-0000-000000000000',
}

json_data = {
    'captchaType': 'HCaptcha',
    'siteKey': '00000000-0000-0000-0000-000000000000',
    'siteReferer': 'https://www.hcaptcha.com/',
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
    "fallbackToActualUA": true
}

response = requests.post('https://api.captcha.run/v2/tasks', headers=headers, json=json_data)

获取识别结果

示例代码和更多信息: 🔗获取任务结果: /v2/tasks/{taskId}

字段含义
gRecaptchaResponse完成验证码后得到的 response,可在您的业务中一并提交到业务服务器
userAgent提交时,您应使用与解决 hCaptcha 时相同的用户代理。

处理中的示例返回

{
  "id": "xxxx",
  "account": "xxxxx",
  "price": "0.012",
  "deducted": false,
  "captchaType": "HCaptcha",
  "request": {
    "captchaType": "HCaptcha",
    "siteKey": "00000000-0000-0000-0000-000000000000",
    "siteReferer": "https://www.hcaptcha.com/",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
    "fallbackToActualUA": true
  },
  "status": "Working",
  "created": "2023-07-27T10:50:26.275Z",
  "ip": "xxx",
  "fromCache": false
}

失败的示例返回

{
  "id": "xxxx",
  "account": "xxxx",
  "price": "0.012",
  "deducted": false,
  "captchaType": "HCaptcha",
  "request": {
    "captchaType": "HCaptcha",
    "siteKey": "00000000-0000-0000-0000-000000000000",
    "siteReferer": "https://www.hcaptcha.com/",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
    "fallbackToActualUA": true
  },
  "reason": "ERROR_CAPTCHA_UNSOLVABLE",
  "status": "Fail",
  "finished": "2023-07-27T10:53:29.170Z",
  "created": "2023-07-27T10:50:26.275Z",
  "ip": "xxx",
  "fromCache": false
}

获取参数

请在 开发者工具->请求 中找到 https://hcaptcha.com/getcaptcha 的请求

request

对应请求参数

  • siteKey: sitekey
  • siteReferer: "https://" + host + "/"

也可以搜索网页源代码

1、打开网页,按F12->Elements,搜索关键词data-sitekey

data-sitekey就是参数websiteKey

<div class="h-captcha" data-sitekey="3ceb8624-1970-4e6b-91d5-70317b70b651" id="hcaptcha"></div>

websiteURL一般就是当前网页的域名

比如你在baidu.com/xxx上看到的,http://baidu.com 就是我们要的参数

注入结果 (模拟)

((response) => {
  for (const ele of document.querySelectorAll("iframe[data-hcaptcha-widget-id]")) {
    const id = ele.attributes["data-hcaptcha-widget-id"].value;
    const data = JSON.stringify({
      source: "hcaptcha",
      label: "challenge-closed",
      id,
      contents: {
        event: "challenge-passed",
        response,
        expiration: 120
      }
    });
    window.dispatchEvent(new MessageEvent('message', {data}))
  }
})('你在平台获取的RESPONSE');