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

相关推荐
studytosky8 分钟前
深度学习理论与实战:Pytorch基础入门
人工智能·pytorch·python·深度学习·机器学习
阿Y加油吧14 分钟前
Java SE核心面试题总结——day 01
java
2021_fc16 分钟前
Flink入门指南:使用Java构建第一个Flink应用
java·大数据·flink
雨白31 分钟前
深入理解 Android DocumentFile:性能陷阱与最佳实践
android
长不大的蜡笔小新37 分钟前
手写数字识别:从零搭建神经网络
人工智能·python·tensorflow
inputA41 分钟前
【LwIP源码学习8】netbuf源码分析
android·c语言·笔记·嵌入式硬件·学习
Java开发追求者1 小时前
vscode导入springboot项目
java·ide·spring boot·vscode
前进的李工1 小时前
LeetCode hot100:094 二叉树的中序遍历:从递归到迭代的完整指南
python·算法·leetcode·链表·二叉树
麦烤楽鸡翅1 小时前
坚持60s (攻防世界)
java·网络安全·jar·ctf·misc·反编译·攻防世界
CHINAHEAO1 小时前
FlyEnv+Bagisto安装遇到的一些问题
android