验证

API文档

概述

发送给DomainTools API的大多数请求需要身份验证。我们支持具有不同安全性的两个身份验证方案。选择最适合您实施的方法。

保护您的API凭据至关重要。即使您以后确定请求是欺诈性的,您的帐户也将对您的密钥进行身份验证的所有查询收取费用。

打开密钥身份验证

这是实施的最简单的身份验证方案,但也是最不安全的。您应该在设计时采取预防措施,以确保您的密钥不会受到损害。

https://api.domaintools.com/v1/domaintools.com?api_username = example&api_key = xxxxxx
必需的参数 价值
api_username 您的API用户名
api_key 您的API键

签名身份验证

HMAC或Hashed消息身份验证代码是我们首选的身份验证方案。它遵循RFC2104中概述的原则,并提供了一种保护API密钥的直接但安全的方法。它涉及传递由您的API用户名,当前日期和时间以及请求URI组成的哈希。然后,使用您的身份验证密钥签名。结果是在短时间后到期的请求,最重要的是,不包含您的身份验证密钥。

必需的参数 价值
api_username 您的API用户名
时间戳 当前时间戳,ISO 8601格式
2011-02-01T14:37:59-0800
2011-02-01T22:37:59Z
2011-02-02T10:37:59+1200
签名 使用MD5,SHA1或SHA256哈希算法的HMAC签名

要创建签名,请使用您的API用户名,ISO 8601格式的时间戳以及您要求的URI构建字符串。然后,使用HMAC函数使用API​​键签名该字符串以给出签名。

在建立签名时遵循以下准则:

  • 1)URI是您请求的api.domaintools.com之后URL的一部分,例如/v1/yourdomain.com/whois
  • 2)时间戳应为ISO 8601格式,并包含时区偏移量
  • 3)哈希和时间戳参数传递给API的时间戳值需要相同
  • 4)确保您的服务器时间准确,并且您始终使用新的时间戳
  • 5)仅使用MD5,SHA1或SHA256进行哈希算法

您可能会在首选的编程语言中找到对HMAC身份验证的内置支持。我们支持几种哈希算法,并仔细遵循HMAC标准,以确保最广泛的支持。

注意:仅出于教育目的提供以下代码样本。

php

<?php class dtsigner {private $ api_username;私人$ api_key;public函数__construct($ api_username,$ api_key){$ this-> api_username = $ api_username;$ this-> api_key = $ api_key;} public函数timestamp(){返回gmdate(“ y-m-d \ th:i:s \ z”);} public函数符号($ TIMESTAMP,$ URI){return hash_hmac('md5',$ this-> api_username。$ timestamp。$ uri,$ uri,$ this-> api_key);}} //使用dtsigner $ api_username ='your_username';$ api_key ='xxxxx-xxxxx-xxxxxx-xxxxxxxxxxxx';$ uri ='/v1/yourdomain.com/whois';$ host ='api.domaintools.com'; $signer = new DTSigner($api_username, $api_key); $timestamp = $signer->timestamp(); $signature = $signer->sign($timestamp, $uri); echo 'https://' . $host . $uri . '?api_username=' . $api_username . '&signature=' . $signature . '×tamp=' . $timestamp; ?>

Python

#!/usr/bin/env Python import import HMAC import hashlib从datetime import import dateTime class dtsigner(对象):def __init __(self,api_username,api_key):self.api_username = api_username = api_username self.api_eymename self.api_key = api_key = api_key defimestamp(reffer)dateTime.utcnow()。strftime('%y-%m-%dt%h:%m:%m:%sz')def sign(self,timestamp,uri):params =''.join([self.api_username,timestamm,timestamp),uri])return hmac.new(self.api_key.encode('utf-8'),params.encode('utf-8'),digestmod = hashlib.sha1).hexdigest()如果__name __name __name __name ==“ __ main__”:api_username ='your_username'api_key ='xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';uri ='/v1/yourdomain.com/whois'host ='api.domaints.com'signer = dtSigner(api_username,api_key)timestamp = signer.timestamp()签名= signate = signer = signer.sign.sign(timestamp,uri,uri)'https'https:https:https:https:https:https:https:// {0} {1}?api_username = {2}&signature = {3}&timestamp = {4}'。格式(host,uri,uri,api_username,signate,signature,timestamp))

通过使用我们的官员,也可以为您处理此身份验证Python API

红宝石

需要“ openssl”类Domaintoodssigner def self.get(用户名,键,uri,uri)timestamp = time.now.utc.strftime(“%y-%m-%m-%dt%h:%m:%m:%m:%sz”)data = username+ username+Timestamp+Uri Digester = openssl :: digest :: digest.new(“ md5”)#可以为sha1 |SHA256签名= openssl :: hmac.hexdigest(digester,key,data)“ api_username =#&signature =&signature =#{signature}&timestamp =#{timestamp}“ end end end api_username ='your_username'your_username';api_key ='xxxxx-xxxxx-xxxxxxxxxxxxxxxxxxxx';uri ='/v1/yourdomain.com/whois';host ='api.domaintools.com';身份验证= domaintoolssigner.get(api_username,api_key,uri)放置“ https://#{host}#{uri}?#{uri}?

爪哇

导入javax.crypto.mac;导入Java.security.Signature Exception;导入javax.crypto.spec.secretkeyspec;导入java.text.simpledateformat;导入java.util.date;公共类DTSIGNER {私有静态最终字符串HMAC_SHA1_ALGORITHM =“ HMACSHA1”;私有字符串api_username;私有字符串api_key;私有SimpleDateFormat TimeFormatter;public dtsigner(字符串api_username,string api_key){this.api_username = api_username; this.api_key = api_key; this.timeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); } public String timestamp() { Date now = new Date(); return this.timeFormatter.format(now); } public String getHexString(byte[] b) { String result = ""; for (int i=0; i < b.length; i++) { result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1); } return result; } public String sign(String timestamp, String uri) throws java.security.SignatureException { String Result; try { String data = new String(this.api_username + timestamp + uri); SecretKeySpec signingKey = new SecretKeySpec(this.api_key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawSignature = mac.doFinal(data.getBytes()); Result = this.getHexString(rawSignature); } catch(Exception e) { throw new java.security.SignatureException("Failed to generate HMAC : " + e.getMessage()); } return Result; } public static void main(String args[]) { String api_username = "your_username"; String api_key = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"; String uri = "/v1/yourdomain.com/whois"; String host = "api.domaintools.com"; try { DTSigner signer = new DTSigner(api_username, api_key); String timestamp = signer.timestamp(); String signature = signer.sign(timestamp, uri); System.out.println("https://" + host + uri + "?api_username=" + api_username + "&signature=" + signature + "×tamp=" + timestamp); } catch(Exception e) { System.out.println("Error trying to sign query"); } } }

C#

使用系统;使用System.io;使用System.Text;使用System.Security.Cryptography;名称空间DTSIGNER {类程序{public static String Timestamp(){return datetime.utcnow.tostring(“ yyyy-mm-ddthh:mm:mm:ssz”);} public static String符号(字符串API_USERNAME,String键,字符串时间戳,字符串URI){asciiencododing encoder = new AsciiienCoding();byte [] data = encoder.getBytes(api_username + timestamp + uri);hmacsha1 hmac = new hmacsha1(encoder.getBytes(key));cryptostream cs = new cryptostream(stream.null,hmac,cryptostreammode.write);cs.write(数据,0,data.engength); cs.Close(); String hex = BitConverter.ToString(hmac.Hash); return hex.Replace("-","").ToLower(); } public static void Main(string[] args) { String api_username = "your_username"; String key = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"; String uri = "/v1/yourdomain.com/whois"; String host = "api.domaintools.com"; String timestamp = Program.timestamp(); String signature = Program.sign(api_username, key, timestamp, uri); Console.WriteLine("https://" + host + uri + "?api_username=" + api_username + "&signature=" + signature + "×tamp=" + timestamp); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }