Skip to content

Instantly share code, notes, and snippets.

@monsterxcn
Created November 26, 2022 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsterxcn/0221ad8fb356488e93d6f6357f1b1030 to your computer and use it in GitHub Desktop.
Save monsterxcn/0221ad8fb356488e93d6f6357f1b1030 to your computer and use it in GitHub Desktop.
[不可用] 原神国服兑换码兑换
import asyncio
import json
import random
import string
import uuid
from hashlib import md5
from time import time
from traceback import format_exc
from typing import Dict, Literal
from httpx import AsyncClient
CLIENT_SALT = "dWCcD2FsOUXEstC5f9xubswZxEeoBOTc"
CLIENT_VERSION = "2.28.1"
CLIENT_TYPE = "2"
AUTHKEY_API = "https://api-takumi.mihoyo.com/binding/api/genAuthKey"
REDEEM_API = "https://hk4e-api.mihoyo.com/common/apicdkey/api/exchangeCdkey"
GAME_VERSION = "CNRELWin3.2.0_R11611027_S11212885_D11793813"
async def queryMihoyo(
cookie: str, aType: Literal["authkey", "redeem"], data: Dict = {}
) -> Dict:
t = str(int(time()))
r = "".join(random.sample(string.ascii_lowercase + string.digits, 6))
m = md5(f"salt={CLIENT_SALT}&t={t}&r={r}".encode()).hexdigest()
headers = {
"content-type": "application/json; charset=UTF-8",
"cookie": cookie,
"ds": f"{t},{r},{m}",
"host": "api-takumi.mihoyo.com",
"referer": "https://app.mihoyo.com",
"user-agent": "okhttp/4.8.0",
"x-rpc-app_version": CLIENT_VERSION,
"x-rpc-channel": "mihoyo",
"x-rpc-client_type": CLIENT_TYPE,
"x-rpc-device_id": str(uuid.uuid3(uuid.NAMESPACE_URL, cookie)),
"x-rpc-device_model": "SM-977N",
"x-rpc-device_name": "Samsung SM-G977N",
"x-rpc-sys_version": "12",
} if aType == "authkey" else {
"Host": "hk4e-api.mihoyo.com",
"User-Agent": "UnityPlayer/2017.4.30f1 (UnityWebRequest/1.0, libcurl/7.51.0-DEV)",
"Accept": "*/*",
# "Accept-Encoding": "*", # "identity",
"X-Unity-Version": "2017.4.30f1"
}
async with AsyncClient() as client:
try:
if aType == "redeem":
res = await client.get(REDEEM_API, headers=headers, params=data)
print(res.text)
rt = res.json()
elif aType == "authkey":
res = await client.post(
AUTHKEY_API,
headers=headers,
content=json.dumps(data, ensure_ascii=False),
)
rt = {"authkey": res.json()["data"]["authkey"]}
else:
raise ValueError(f"未知的请求类型:{aType}")
except Exception as e:
print(f"米游社 {aType} 请求失败:{e.__class__.__name__}\n{format_exc()}")
return {"error": f"[{e.__class__.__name__}] 未能成功{aType}!"}
return rt
async def redeemCN():
cookie = "stoken=q0cuxxxxxVGoZmxxxxx34MxxxxxtAYIuzY2xxx5T; stuid=28xxxxx93"
data = {
"auth_appid": "apicdkey", # webview_gacha
"game_biz": "hk4e_cn",
"game_uid": "134266985",
"region": "cn_gf01",
}
authkeyRes = await queryMihoyo(cookie, "authkey", data)
if not authkeyRes.get("authkey"):
return "生成密钥失败!"
# authkeyRes = {"authkey": 'xXuaTHj0IBG2ApTF9ZGlmpFSCgYmRN9+06fkXNCUqJgBnIssAD3Iq0tA0LzRLUV+lmQ6LoDb/0wFylbgcbL2V1Pm1qp5g6uzkXWqoESb2IzjgEbXkOlDmEyFMVPaDN6IMPHTCZswWB3nh3ynydO7yx7jpGw1lBoxb6Jglz63f6tdj+/IYzdhrkPERqOoHKmNI2EIZYBzmTUqg2dOcvWH6Mg4WBFQtOg9Rn7njTIvXleUbKGYrnwNbW0KBPegSLcjtedifLVOKkO6ekOBe5jmrEEV7NXpYSnRDaCcUtyjinG1kEMlWQGJnF+H1BUIh4fzjQrjrXWEcldpaUL9jylj7g=='}
data = {
"sign_type": "2",
"auth_appid": "apicdkey",
"authkey_ver": "1",
"cdkey": "CDKEYHERE", # 兑换码
"lang": "zh-cn",
"device_type": "pc",
# "ext": "",
"game_version": GAME_VERSION,
"plat_type": "pc",
"authkey": authkeyRes["authkey"],
"game_biz": "hk4e_cn",
}
redeemRes = await queryMihoyo("", "redeem", data)
print(redeemRes.get("error") or "结束!")
# -1033 authkey error
# -1035 auth_appid error?
asyncio.run(redeemCN())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment