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

相关推荐
nil1 分钟前
shortcutkey:跨平台快捷键管理工具的设计与实现
python·开源·github
攻城狮杰森3 分钟前
Eudic → Maimemo 自动同步工具:欧路词典 & 墨墨背单词
python·api·shell·ai编程·欧路词典·墨墨本单词
( •̀∀•́ )9206 分钟前
高性能拖拽排序
java·开发语言·算法
打工人1117 分钟前
安卓Android 获取mac地址及sn
android·macos
用户0332126663678 分钟前
Word文档中插入图片:使用 Spire.Doc for Java实现自动化与精细控制
java
Vince的修炼之路8 分钟前
用Python将JSON格式文件数据导入到Elasticsearch上
python
不会吉他的肌肉男不是好的挨踢男11 分钟前
LLaMA Factory 训练模型未检测到CUDA环境解决
python·ai·llama
sheji341614 分钟前
【开题答辩全过程】以 基于springboot游泳馆管理系统为例,包含答辩的问题和答案
java·spring boot·后端
Yang-Never20 分钟前
Open GL ES->EGL渲染环境、数据、引擎、线程的创建
android·java·开发语言·kotlin·android studio
Justinyh21 分钟前
Notion同步到CSDN + 构建Obsidian本地博客系统指南
python·csdn·图床·notion·obsidian·文档同步·piclist