接入教程
1. 注册GetOTP账户并获取API密钥。
2. 调用发送接口,指定接收方式和模板。
3. 用户接收验证码并提交。
4. 调用验证接口核对验证码。
5. 根据返回结果处理认证逻辑。
使用 Python 发送短信验证码
python
import requests
url = 'https://api.example.com/v1/otp/send'
payload = {
'channel': 'sms',
'recipient': '+8612345678900',
'template': 'login'
}
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
response = requests.post(url, json=payload, headers=headers)
print(response.status_code)
print(response.json())
使用 PHP 验证一次性密码
php
<?php
$url = 'https://api.example.com/v1/otp/verify';
$data = [
'code' => '123456',
'reference' => 'REF_001'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer YOUR_API_KEY',
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
使用 JavaScript 发送邮件验证码
javascript
const url = 'https://api.example.com/v1/otp/send';
const payload = {
channel: 'email',
recipient: 'user@example.com',
template: 'reset_password'
};
fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
常见问题
如何获取 API 密钥?
请访问官方网站 https://otp.dev/en/docs/ 注册账户,然后在控制台创建项目即可获取 API 密钥。
验证码的有效期是多久?
默认情况下,验证码的有效期为 5 分钟。您可以在 API 请求中通过参数自定义有效期。
支持哪些发送渠道?
GetOTP API 目前支持短信和邮件两种发送渠道,您可以根据需求选择合适的渠道。
Aitishiku.com