Python实现接口签名调用

目录:

1、第三方接口签名调用

python 复制代码
import json
import requests
import hashlib
import time
import hmac
access_key = 'xxxxxxxxxxxxxxx'
secret_key = 'xxxxxxxxxxxxxxx'
# 应用信息
def _wps4_sig(method, url, date, body):    
    print(body)
    if body is None:
        bodySha = ""
    else:
        bodySha = hashlib.sha256(body.encode('utf-8')).hexdigest()

    content = "xxx-4" + method + url + "application/json" + date + bodySha
    print(content)
    signature = hmac.new(secret_key.encode('utf-8'), content.encode('utf-8'), hashlib.sha256).hexdigest()

    return "xxx-4 %s:%s" % (access_key, signature)


def wps4_request(method, host, uri, body=None, cookie=None, headers=None):
    requests.packages.urllib3.disable_warnings()

    if body is not None and not isinstance(body, str):
        body = json.dumps(body)

    date = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
    # date = "Fri, 03 Jan 2025 07:44:40 GMT"

    # print date
    header = {"Content-type": "application/json"}
    header['xxx-Docs-Date'] = date
    header['xxx-Docs-Authorization'] = _wps4_sig(method, uri, date, body)
    # header['xxx-Docs-Authorization'] ='xxx-4 LSURICXMDHRWXHKR:c7451af3b5318781ab323817d2411a8e40d6d5ae86a06b86676b0e6b7928579e'

    if headers != None:
        # header = {}
        for key, value in headers.items():
            header[key] = value

    url = "%s%s" % (host, uri)
    r = requests.request(method, url, data=body,
                         headers=header, cookies=cookie, verify=False)
    return r.status_code, r.text
def edit(file_id=1):
    url = '/api/edit/v1/files/{0}/link?type=w'.format(file_id)
    #result = wps4_request('post', 'https://core.equiclouds.com', '/www/dd/test/req_raw', '''{"a":1}''')
    #print(result[1])
    result = wps4_request('GET', 'http://xx.xx.xx.xxx:xxxx/open', url)
    print(result[0])
    print(result[1])
edit()

2、调用结果

相关推荐
Nebula_g4 分钟前
JavaSE基础语法:面向对象高级(代码中的成分)
java·开发语言·编程·javase·技术栈·高级语法
for_ever_love__13 分钟前
python基础语法学习: 异常的传递性
开发语言·python·学习
YM52e1 小时前
分页查询的基石:ArkTS 为鸿蒙商品列表设计 LIMIT/OFFSET 的表
android·学习·华为·harmonyos
麻雀飞吧2 小时前
零基础选量化工具,先把问题说清楚
人工智能·python
段一凡-华北理工大学2 小时前
AI推动工业智能化转型~系列文章20:工业 AI 平台架构:云-边-端协同的技术体系
人工智能·python·架构·工业平台·云-边协同
CodeBlog-star2 小时前
Harness Engineering:Pi Agent 架构深度解析
人工智能·python·架构·harness工程
简不变2 小时前
将components下的文件夹复制到别一个文件夹下,并重新生成CMakelist.txt文件
python
小强库计算机毕业设计2 小时前
SpringBoot+Vue3 学生宿舍管理系统实战
java·spring boot·后端·vue·学生宿舍管理系统
李冰然2 小时前
深入Java集合框架:HashMap源码解析(JDK 8)
java
安卓与AI研习社2 小时前
Android ANR 到底怎么定位?从触发原理到日志判断的完整方法
android