PDF转docx报错代码30003

阅读次数 5

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去掉了

2 Answers

建议您可以检查下你request中的url是否带有域名和http/https协议哈

get_uri = "/api/developer/v1/tasks/convert/to/docx/b4058881d1254de285cefc8f88e53a3c" conn.request("GET", get_uri, headers=headers) 但我conn.request传入的是相对路径get_uri啊 我计算md5也是用get_uri

我通过后台日志查看到请求的http 原生request 的url的内容是uri,导致出现了这样的报错,建议您可以自查下python sdk这种使用方法是否会出现这样的问题哈