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

相关推荐
要开心吖ZSH3 分钟前
应用集成平台-系统之间的桥梁-思路分享
java·kafka·交互
TsengOnce6 分钟前
阿里云ECS多版本JDK切换
java·python·阿里云
Hi_kenyon7 分钟前
FastAPI+VUE3创建一个项目的步骤模板(三)
python·fastapi
wearegogog1238 分钟前
基于C#的FTP客户端实现方案
java·网络·c#
听风吟丶10 分钟前
Java NIO 深度解析:从核心组件到高并发实战
java·开发语言·jvm
野生技术架构师16 分钟前
Java面试题及答案总结(互联网大厂新版)
java·面试·状态模式
a努力。17 分钟前
小红书Java面试被问:ThreadLocal 内存泄漏问题及解决方案
java·jvm·后端·算法·面试·架构
此生只爱蛋18 分钟前
【Redis】String 字符串
java·数据库·redis
C++业余爱好者18 分钟前
Java开发中Entity、VO、DTO、Form对象详解
java·开发语言
Flyora21 分钟前
【踩坑实录】PyInstaller 打包 “找不到库的完整解决方案
python