唉,还是不行啊,这是代码:import os
import time
import hmac
import hashlib
import base64
import urllib
from flask import Flask, request
app = Flask(__name__)
SECRET = 'mySecret' # 替换为您的签名密钥
def generate_signature(timestamp):
timestamp_str = str(timestamp)
secret_enc = SECRET.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp_str, SECRET)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
signature = urllib.parse.quote(base64.b64encode(hmac_code), safe='')
# signature = base64.b64encode(hmac_code).decode()
# url_encoded_signature = urllib.parse.quote(signature, safe='')
return signature
@app.route('/webhook', methods=['POST'])
def handle_webhook():
# 验证请求来源是否是Gitee
if request.headers.get('User-Agent') == 'git-oschina-hook':
# 验证签名
timestamp = int(request.headers.get('X-Gitee-Timestamp'))
actual_signature = request.headers.get('X-Gitee-Token')
expected_signature = generate_signature(timestamp)
if actual_signature != expected_signature:
return '', 403 # 返回拒绝访问状态码
# 在这里编写处理Webhook请求的代码逻辑
data = request.json
print('Received webhook request:', data)
# 执行 git pull、pnpm install 和 pnpm build 等操作
# ...
return '', 200 # 返回成功状态码
else:
return '', 403 # 返回拒绝访问状态码
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3002)
这是flask日志:127.0.0.1 - - [26/Oct/2023 22:20:08] "POST /webhook?sign=%200CTC8AsNbKn%20SZde1kyMYsnIP0b6nJ8IWoO7pqn754%3D×tamp=1698330008081 HTTP/1.1" 403 -
这是Gitee后台header信息:Request URL: https://xxx.com/webhook
Request Method: POST
X-Gitee-Token: +0CTC8AsNbKn+SZde1kyMYsnIP0b6nJ8IWoO7pqn754=
X-Gitee-Event: push_hooks
User-Agent: git-oschina-hook
X-Gitee-Timestamp: 1698330008081
X-Gitee-Ping: true
Content-Type: application/json
X-Git-Oschina-Event: push_hooks
|