发出 API 请求时,使用服务器到服务器 OAuth 向 WhoisXML API 进行身份验证。这也称为双向 OAuth,因为它使用两步流程,不需要用户交互即可进行身份验证或授权。以下是流程。
您还可以在 GitHub 上查看此脚本,了解如何将 WHOIS API 与服务器端 SSO 结合使用的示例。
使用访问令牌授权类型生成访问口令。此授权类型的特点如下:
请注意,生成的accessToken将代替API请求中的apiKey参数。
curl --location 'https://main.whoisxmlapi.com/oauth/token' \
--header 'Authorization: Bearer %base64_encoded_API_key%' \
--header 'Content-Type: application/json' \
--data '{
"grantType": "access_token",
"expiresIn": 7200
}'
授权 |
授予类型 |
必填。生成访问令牌的授权类型。 可接受的值: access_token 。 |
到期时间 |
选填。访问口令牌的有效期(以秒为单位)。 可接受的值: 1800 、 3600 、 7200 、 10800 。 默认值: 3600 。 |
输出格式 |
选填。响应输出格式。 可接受的值: JSON | XML 默认值: JSON |
{
"accessToken": "G2OIE2AKRCVDYFUJCV5PXXXXXXXXXXXX",
"expiresIn": 3600
}
使用响应中的accessToken值来验证您的 API 请求。将结果值替换到apiKey字段中,就像使用没有 OAuth 的普通 API 密钥一样。
{
"code": 401,
"messages": "Access restricted. Check the credits balance or enter the correct API key."
}
{
"code": 422,
"messages": {
"grantType": [
"The selected grant type is invalid."
]
}
}
curl --location 'https://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=google.com' \ --header 'Authorization: Bearer %accessToken%'
curl --location 'https://www.whoisxmlapi.com/whoisserver/WhoisService' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer %accessToken%' \
--data '{
"domainName": "google.com"
}'