python中调用接口pdf转docx报错如下:{"code":30003,"message":"InvalidSignature","hint":"unsupported protocol scheme ""","extra":""}
以下为我的代码:
import hashlib
import hmac
import email.utils
import http.client
def calculate_md5(data: str) -> str:
"""
Calculate MD5 hash of the given data and return it as a lowercase hexadecimal string.
"""
md5_hash = hashlib.md5(data.encode('utf-8')).hexdigest()
return md5_hash
def get_request_md5(uri: str) -> str:
"""
For GET requests, calculate MD5 hash of the URI.
"""
return calculate_md5(uri)
def calculate_sha1(key: str, data: str) -> str:
"""
Calculate SHA1 hash using HMAC with the given key and data.
"""
sha1_hash = hmac.new(key.encode('utf-8'), data.encode('utf-8'), hashlib.sha1).hexdigest()
return sha1_hash
Application details
app_id = '' app_key = ''
GET request URI
get_uri = "/api/developer/v1/tasks/convert/to/docx/bdc88720c3f549378456213a4da13a23"
Current date in RFC 2822 format
current_time = email.utils.formatdate(usegmt=True) print("Date:", current_time)
Calculate Content-MD5 for the URI
content_md5 = get_request_md5(get_uri) print("Content-MD5:", content_md5)
Content-Type for the request
content_type = "application/json"
Calculate the string to be hashed for Authorization
string_to_sign = app_key + content_md5 + content_type + current_time print("String to Sign:", string_to_sign)
Calculate SHA1 hash for the Authorization header
signature = calculate_sha1(app_key, string_to_sign) print("Signature:", signature)
Generate Authorization header
authorization_header = f"WPS-2:{app_id}:{signature}" print("Authorization:", authorization_header) conn = http.client.HTTPSConnection("solution.wps.cn")
headers = {
'Date': current_time,
'Content-Md5': content_md5,
'Content-Type': content_type,
'Authorization': authorization_header
}
conn.request("GET", get_uri, headers=headers)
res = conn.getresponse() data = res.read()
print(data.decode("utf-8")) 其中api_id和api_key去掉了