API获取域名列表一直AuthFailure,code:4100?

  • 回答 (1)
  • 关注 (0)
  • 查看 (26)
import hmac
import hashlib
import json
import requests
import time
from hashlib import sha1
import base64
from urllib.parse import quote
with open("config.json") as config_file:
    config = json.load(config_file)  
uri = "cns.api.qcloud.com/v2/index.php?"
SecretId = config["SecretId"]
SecretKey = config["SecretKey"]

# 所需要的参数
param_dict = {
    "Action": "DomainList",
    "Nonce": "233333",
    "SecretId": SecretId,
    "Timestamp": str(int(time.time()))

}

# 生成请求字符串
result_str = ("Action=" + param_dict["Action"]
              + "&Nonce="+param_dict["Nonce"]
              + "&SecretId="+param_dict["SecretId"]
              + "&Timestamp="+param_dict["Timestamp"]
              )

# 拼接签名原文字符串
requestStr = "GET"+uri+result_str

# 签名
signStr = requestStr.encode("UTF-8")
SecretKey = SecretKey.encode("UTF-8")
hashed = hmac.new(SecretKey, signStr, sha1).hexdigest()

# base64加密,同时进行URL编码
base64Data = quote(base64.b64encode(hashed.encode("utf-8")))

data = requests.get("https://"+uri+result_str+"&Signature="+base64Data).text
print(data)


# -------------------输出------------------
# {
# "code":4100,
# "message":"\u8eab\u4efd\u8ba4\u8bc1\u5931\u8d25
#            \uff0c\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u5931\u8d25",
# "codeDesc":"AuthFailure"
# }
# ------------------------------------------
用户6879011用户6879011提问于
zqfan回答于
推荐

推荐使用SDK进行调用:https://github.com/QcloudApi/qcloudapi-sdk-python

如果要自己写,也请参考下签名文档中python的代码示例,或者SDK中的源码:https://github.com/QcloudApi/qcloudapi-sdk-python/blob/master/QcloudApi/common/sign.py

API2.0不再推荐使用了,请尽量用API3.0吧

可能回答问题的人

  • zqfan

    21 粉丝0 提问216 回答
  • 骑牛看晨曦

    4 粉丝522 提问1 回答
  • 最爱开车啦

    8 粉丝503 提问1 回答
  • 用户2366882

    0 粉丝0 提问1 回答

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励