xpengp

前言

使用阿里云产品,调用API接口一般有两种方式,使用APPCODE或使用ak、sk生成认证签名。
第二种比较复杂,今天记录一下。

代码

  1. 调用阿里云接口,需要在headers中添加签名(X-Ca-Signature字段)
  2. 以whois查询接口为例 (http://whois.market.alicloudapi.com/icredit_ai_seo/whois/v1)
  3. 阿里云生成认证字符串文档
  4. 以nodejs为例
const AppKey = \'your AK\';
const AppSecret = \'your SK\';
const domainName = \'your domain name\';

const apiUrl = `http://whois.market.alicloudapi.com/icredit_ai_seo/whois/v1?STRING=${domainName}`;

const headers = {
        \'Accept\': \'application/json\',
        \'X-Ca-Key\': AppKey,
        \'X-Ca-Signature\': getALYSignature(domainName, AppSecret, \'/icredit_ai_seo/whois/v1\')
    };
// path 除根路径以外的其它部分
function getALYSignature(domainName, AppSecret, path) {
    let StringToSign = \'GET\' + \'\n\' + \'application/json\' + \'\n\' + \'\n\' + \'\n\' + \'\n\' + path + \'?STRING=\' + domainName;
    return crypto
        .createHmac(\'sha256\', AppSecret)
        .update(StringToSign)
        .digest()
        .toString(\'base64\'); // 这里使用base64 不是hex
}

总结

  1. 使用阿里云的whois接口时,有的域名查询需要加www.才可以查询,有的不需要(规律我也找不到)。
  2. 通常返回结果为空时,都是查询出错了。错误代码一般都在返回的headers中,对照官方文档查看即可。

分类:

技术点:

相关文章:

  • 2021-09-10
  • 2021-11-20
  • 2021-08-31
  • 2021-10-17
  • 2021-08-05
猜你喜欢
  • 2021-11-01
  • 2021-12-20
  • 2021-07-10
  • 2022-01-02
  • 2021-07-14
  • 2021-12-03
相关资源
相似解决方案