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、调用结果

相关推荐
米码收割机9 小时前
【项目】spring boot+vue3 宠物领养系统(源码+文档)【独一无二】
java·spring boot·宠物
叶 落9 小时前
Mac 通过 Miniconda 安装 Python
python·macos·conda·miniconda
刹那芳华199210 小时前
STMF+ESP-S+MQTT协议连接华为云端(附踩坑记录)
java·struts·华为
就改了15 小时前
Java8 日期处理(详细版)
java·python·算法
麻瓜生活睁不开眼16 小时前
Android16定制SearchLauncher新增投屏Cast桌面快捷图标完整实现
java·launcher·aosp16
ii_best17 小时前
更新!移动端开发软件按键安卓版&手机助手v5.1.0上线!本地AI识别全面解锁,脚本开发再升级
android·人工智能·ios·按键精灵
IT小盘17 小时前
12-Prompt不等于一句话-System-User-Context三层结构
java·windows·prompt
方乐寺村18 小时前
s,使用libpng提升png图片的保存速度。接下来本文将阐述在Android中如何集成libpng,以及在使用过程中遇到的问题和最 ...
android
威联通安全存储18 小时前
TS-h1887XU-RP在汽车发动机热试与NVH在线质检网中的部署
python·汽车
name好难取诶18 小时前
PHP 静态分析工具实战 PHPStan 和 Psalm 完全指南
android·开发语言·php