Spaces:
Paused
Paused
Update bearer_token.py
Browse files- bearer_token.py +20 -55
bearer_token.py
CHANGED
|
@@ -1,61 +1,26 @@
|
|
| 1 |
-
import hmac
|
| 2 |
-
import hashlib
|
| 3 |
import base64
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
from datetime import datetime, timezone
|
| 6 |
|
| 7 |
class BearerTokenGenerator:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
timestamp = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
| 22 |
-
prefix = f"POST:{self.path}:{timestamp}\n".encode('utf-8')
|
| 23 |
-
|
| 24 |
-
# 生成签名
|
| 25 |
-
signature = self.generate_signature(prefix + body)
|
| 26 |
-
|
| 27 |
-
# 编码secretAuthPrefix
|
| 28 |
-
encoded_secret_auth_prefix = base64.b64encode(self.secret_auth_prefix).decode('utf-8')
|
| 29 |
-
|
| 30 |
-
# 构建Bearer Token
|
| 31 |
-
bearer_token = f"Bearer {encoded_secret_auth_prefix}.{signature}"
|
| 32 |
-
|
| 33 |
return bearer_token, timestamp
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"Client-time-zone": "-04:00",
|
| 40 |
-
"Authorization": bearer_token,
|
| 41 |
-
"User-Agent": f"ChatOn_Android/{self.api_version}",
|
| 42 |
-
"Accept-Language": "en-US",
|
| 43 |
-
"X-Cl-Options": "hb",
|
| 44 |
-
"Content-Type": "application/json; charset=UTF-8"
|
| 45 |
-
}
|
| 46 |
-
response = requests.post(self.base_url + self.path, headers=headers, data=body)
|
| 47 |
-
return response.text
|
| 48 |
-
|
| 49 |
-
# 示例使用
|
| 50 |
-
if __name__ == "__main__":
|
| 51 |
-
secret_key = bytes([14, 94, 79, 102, 38, 245, 11, 65, 100, 43, 115, 94, 15, 241, 14, 16, 66, 129, 248, 226, 98, 109, 235, 60, 62, 41, 78, 29, 72, 181, 47, 8])
|
| 52 |
-
secret_auth_prefix = bytes([252, 137, 185, 155, 127, 94, 106, 81, 69, 242, 189, 184, 26, 228, 174, 239])
|
| 53 |
-
path = "/chats/stream"
|
| 54 |
-
base_url = "https://example.com/api" # 替换为实际的BaseURL
|
| 55 |
-
api_version = "1.0"
|
| 56 |
-
|
| 57 |
-
body = '{"key": "value"}'.encode('utf-8')
|
| 58 |
-
|
| 59 |
-
generator = BearerTokenGenerator(secret_key, secret_auth_prefix, path, base_url, api_version)
|
| 60 |
-
response = generator.make_request(body)
|
| 61 |
-
print(response)
|
|
|
|
|
|
|
|
|
|
| 1 |
import base64
|
| 2 |
+
import hashlib
|
| 3 |
+
import hmac
|
| 4 |
+
import time
|
| 5 |
from datetime import datetime, timezone
|
| 6 |
|
| 7 |
class BearerTokenGenerator:
|
| 8 |
+
SECRET_AUTH_PREFIX = bytes([252, 137, 185, 155, 127, 94, 106, 81, 69, 242, 189, 184, 26, 228, 174, 239])
|
| 9 |
+
SECRET_KEY = bytes([14, 94, 79, 102, 38, 245, 11, 65, 100, 43, 115, 94, 15, 241, 14, 16, 66, 129, 248, 226, 98, 109, 235, 60, 62, 41, 78, 29, 72, 181, 47, 8])
|
| 10 |
+
|
| 11 |
+
@staticmethod
|
| 12 |
+
def get_bearer(body: str, path: str = "/chats/stream") -> tuple:
|
| 13 |
+
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 14 |
+
prefix = f"POST:{path}:{timestamp}\n".encode()
|
| 15 |
+
to_sign = prefix + body.encode()
|
| 16 |
+
|
| 17 |
+
signature = BearerTokenGenerator.generate_signature(to_sign)
|
| 18 |
+
auth_prefix_base64 = base64.b64encode(BearerTokenGenerator.SECRET_AUTH_PREFIX).decode()
|
| 19 |
+
|
| 20 |
+
bearer_token = f"Bearer {auth_prefix_base64}.{signature}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return bearer_token, timestamp
|
| 22 |
|
| 23 |
+
@staticmethod
|
| 24 |
+
def generate_signature(to_sign: bytes) -> str:
|
| 25 |
+
h = hmac.new(BearerTokenGenerator.SECRET_KEY, to_sign, hashlib.sha256)
|
| 26 |
+
return base64.b64encode(h.digest()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|