Authorization头部需要SignedHeaders参数
创始人
2024-09-22 15:31:47
0

在API请求中添加“SignedHeaders”参数的值。下面是一个AWS4签名的示例:

import hmac, hashlib, base64

def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()

def get_signature_key(key, date_stamp, region_name, service_name):
    k_date = sign(('AWS4' + key).encode('utf-8'), date_stamp)
    k_region = sign(k_date, region_name)
    k_service = sign(k_region, service_name)
    k_signing = sign(k_service, 'aws4_request')
    return k_signing

def get_auth_header(access_key, secret_key, method, endpoint, path, query_params, headers, payload, region):
    timestamp = '20210615T104237Z'
    datestamp = '20210615'
    HTTPRequestMethod = method.upper()
    CanonicalURI = path
    CanonicalQueryString = '&'.join(sorted(query_params))
    CanonicalHeaders = 'host:' + endpoint + '\n' + 'x-amz-date:' + timestamp + '\n'
    SignedHeaders = 'host;x-amz-date'
    payload_hash = hashlib.sha256(payload.encode('utf-8')).hexdigest()
    CanonicalRequest = HTTPRequestMethod + '\n' + CanonicalURI + '\n' + CanonicalQueryString + '\n' + CanonicalHeaders + '\n' + SignedHeaders + '\n' + payload_hash
    algorithm = 'AWS4-HMAC-SHA256'
    credential_scope = datestamp + '/' + region + '/s3/aws4_request'
    string_to_sign = algorithm + '\n' +  timestamp + '\n' +  credential_scope + '\n' +  hashlib.sha256(CanonicalRequest.encode('utf-8')).hexdigest()
    signing_key = get_signature_key(secret_key, datestamp, region, 's3')
    signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
    Authorization = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' +  'SignedHeaders=' + SignedHeaders + ', ' + 'Signature=' + signature
    
    headers.update({'x-amz-date': timestamp, 'Authorization': Authorization})
    return headers

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...