视觉智能平台签名python代码是这样处理吗?
{'RequestId': '2C8DA635-2835-5C23-A3B2-B44AB6AADA22', 'Message': 'Specified signature is not matched with our calculation. server string to sign is:POST&%2F&AccessKeyId%3DLTAI5tNthhnd4x1fmYV6VNyW%26Action%3DInteractiveFullSegmentation%26Format%3DJSON%26ImageUrl%3Dhttp%253A%252F%252Fviapi-test.oss-cn-shanghai.aliyuncs.com%252Fviapi-3.0domepic%252Faigen%252FInteractiveFullSegmentation%252FInteractiveFullSegmentation1.png%26RegionId%3Dcn-shanghai%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3Db26294f0-e68e-11ee-a607-50ebf63d8db6%26SignatureVersion%3D1.0%26Timestamp%3D2024-03-20T07%253A51%253A44Z%26Version%3D2024-01-11', 'Recommend': 'https://api.aliyun.com/troubleshoot?q=SignatureDoesNotMatch&product=aigen&requestId=2C8DA635-2835-5C23-A3B2-B44AB6AADA22', 'HostId': 'aigen.cn-shanghai.aliyuncs.com', 'Code': 'SignatureDoesNotMatch'}
我的签名方法:
import hmac
import base64
from hashlib import sha1
def hash_hmac(code, key, sha1):
hmac_code = hmac.new(key.encode(), code.encode(), sha1).digest()
return base64.b64encode(hmac_code).decode()
根据您提供的信息,您的签名方法看起来是正确的。但是,您需要确保在调用视觉智能平台的接口时,传递正确的参数和签名。以下是一个使用Python的示例代码:
import requests
import json
import hmac
import base64
from haib import sha1
def hash_hmac(code, key, sha1):
hmac_code = hmac.new(key.encode(), code.encode(), sha1).digest()
return base64.b64encode(hmac_code).decode()
# 设置请求参数
url = "https://aigen.cn-shanghai.aliyuncs.com"
access_key_id = "LTAI5tNthhnd4x1fmYV6VNyW"
action = "InteractiveFullSegmentation"
format = "JSON"
image_url = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/aigen/InteractiveFullSegmentation/InteractiveFullSegmentation1.png"
region_id = "cn-shanghai"
signature_method = "HMAC-SHA1"
signature_nonce = "b26294f0-e68e-11ee-a607-50ebf63d8db6"
signature_version = "1.0"
timestamp = "2024-03-20T07:51:44Z"
version = "2024-01-11"
# 生成签名
string_to_sign = f"POST&%2F&AccessKeyId%3D{access_key_id}&Action={action}&Format={format}&ImageUrl={image_url}&RegionId={region_id}&SignatureMethod={signature_method}&SignatureNonce={signature_nonce}&SignatureVersion={signature_version}&Timestamp={timestamp}&Version={version}"
signature = hash_hmac(string_to_sign, access_key_id, sha1)
# 发送请求
headers = {
"Content-Type": "application/json",
"Signature": signature,
"AccessKeyId": access_key_id,
"SignatureMethod": signature_method,
"SignatureVersion": signature_version,
"Timestamp": timestamp,
"Version": version
}
data = {
"Action": action,
"Format": format,
"ImageUrl": image_url,
"RegionId": region_id,
"SignatureNonce": signature_nonce
}
response = requests.post(url, headers=headers, data=json.dumps(data))
# 处理响应
if response.status_code == 200:
result = response.json()
print("成功获取结果:", result)
else:
print("请求失败,状态码:", response.status_code)
请确保将上述代码中的access_key_id
替换为您的实际访问密钥ID。此外,根据您的需求调整其他参数。
这里有的转化的不对,比如这个正常应该是&,但被转化了,可以看下这个完整的待签名的请求字符串示例:POST&%2F&AccessKeyId%3DyourAccessId&Action%3DMakeSuperResolutionImage&Format%3DJSON&RegionId%3Dcn-shanghai&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3D4a816d44-6186-4f7e-a45f-ba1b3ed73aed&SignatureVersion%3D1.0&Timestamp%3D2019-12-07T13%253A28%253A52Z&Url%3Dhttp%253A%252F%252Fviapi-demo.oss-cn-shanghai.aliyuncs.com%252Fviapi-demo%252Fimages%252FMakeSuperResolution%252Fsup-dog.png&Version%3D2019-09-30
此回答整理自钉群“阿里云视觉智能开放平台咨询1群”
在阿里云等云服务中,使用HTTP请求调用API时通常需要对请求进行签名以验证请求的合法性。对于上述错误消息"SignatureDoesNotMatch",意味着您生成的签名与阿里云服务器计算得到的签名不符,所以请求被拒绝。
在阿里云API签名的过程中,通常遵循以下步骤:
import hashlib
import hmac
import base64
import urllib.parse
import time
# 定义你的Access Key Secret
access_key_secret = 'your_access_key_secret'
# 构建待签名字符串
method = 'POST'
endpoint = 'aigen.cn-shanghai.aliyuncs.com'
service = '/'
# 参数字典(根据实际请求构建)
params = {
'Action': 'InteractiveFullSegmentation',
'Format': 'JSON',
'ImageUrl': 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/...',
'RegionId': 'cn-shanghai',
'SignatureMethod': 'HMAC-SHA1',
'SignatureNonce': 'b26294f0-e68e-11ee-a607-50ebf63d8db6',
'SignatureVersion': '1.0',
'Timestamp': '2024-03-20T07:51:44Z',
'Version': '2024-01-11',
}
# 将参数排序并URL编码后拼接成待签名字符串
sorted_params = sorted(params.items(), key=lambda x: x[0])
canonicalized_query_string = '&'.join(f"{k}={urllib.parse.quote(v)}" for k, v in sorted_params)
# 构建规范化的请求字符串
string_to_sign = f"{method}&{urllib.parse.quote_plus(service)}&{urllib.parse.quote_plus(canonicalized_query_string)}"
# 使用Access Key Secret和HMAC-SHA1算法生成签名
hmac_code = hmac.new(access_key_secret.encode('utf-8'),
string_to_sign.encode('utf-8'),
hashlib.sha1).digest()
# 将签名结果Base64编码
signature = base64.b64encode(hmac_code).decode('utf-8')
# 最后将签名添加到请求参数中
params['Signature'] = signature
# 然后按照官方文档的要求构造完整的请求URL或HTTP请求头
请注意,上述代码片段仅作示例用途,实际使用时需要根据阿里云官方文档中给出的规则正确构造待签名字符串(即 string_to_sign
)。此外,Timestamp
字段应当实时获取当前时间戳以避免签名失效。最后,确保Access Key Secret已正确配置,并且在生成签名时不要添加额外的换行符或其他非ASCII字符。
根据您提供的错误信息,您在尝试使用阿里云视觉智能平台时遇到了“SignatureDoesNotMatch”的错误,这意味着您生成的签名与阿里云计算出的签名不匹配。您给出的签名方法看起来是基于HMAC-SHA1的签名算法。确保您的签名过程正确包含了请求方法、URL路径、AccessKeyId、Timestamp、SignatureNonce以及所有请求参数,并按正确的规则排序和编码后参与签名计算。建议参照阿里云官方文档中关于签名生成的具体步骤和示例来检查和修改您的签名代码。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。