Skip to content

Instantly share code, notes, and snippets.

@Konano
Last active April 29, 2024 01:47
Show Gist options
  • Save Konano/43f9090c9bf077825dcb4771ea5bfa04 to your computer and use it in GitHub Desktop.
Save Konano/43f9090c9bf077825dcb4771ea5bfa04 to your computer and use it in GitHub Desktop.
利用华为的「寻找设备」功能定位手机
import requests
import http.cookiejar as HC
import re, json
import traceback
MOBILE = {
'deviceid': 'xxxxxx',
'hwid_cas_sid': 'xxxxxx',
'useraccount': 'xxxxxx',
'password': 'xxxxxx'
}
session = requests.session()
session.cookies = HC.LWPCookieJar(filename='cookies')
try:
session.cookies.load(ignore_discard=True)
except Exception:
pass
deviceParam = {
'deviceId': MOBILE['deviceid'],
'deviceType': 9,
}
def get_location():
global executeTime
try:
try:
headers = {'Content-Type': 'application/json'}
res = session.post('https://cloud.huawei.com/mobile/locate', headers=headers, data=json.dumps(deviceParam), timeout=(5,10))
assert res.text[:1] == '{'
print(res.json())
res = session.post('https://cloud.huawei.com/mobile/queryLocateResult', headers=headers, data=json.dumps(deviceParam), timeout=(5,10))
assert res.text[:1] == '{'
mobileInfo = res.json()
print(mobileInfo)
assert mobileInfo['resultCode'] == '0'
except AssertionError:
print(traceback.format_exc())
headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
cookies = {
'hwid_cas_sid': MOBILE['hwid_cas_sid'],
}
params = {
'service': 'https://cloud.huawei.com:443/others/login.action',
'loginChannel': 1000002,
'reqClientType': 1,
'userAccount': int(MOBILE['useraccount']),
'password': MOBILE['password'],
'lowLogin': True,
}
res = session.post('https://id1.cloud.huawei.com/CAS/IDM_W/ajaxHandler/remoteLogin', headers=headers, cookies=cookies, params=params, timeout=(5,10))
print(res.text)
res = session.get(res.json()['callbackURL'], timeout=(5,10))
print(res.url)
headers = {
'Interfaceversion': 'v3',
# 'Fromloginauth': 'false',
'Content-Type': 'application/x-www-form-urlencoded'
}
data = re.findall(r"authorize\?(.*)$", res.url)[0]
print(data)
res = session.post('https://oauth-login.cloud.huawei.com/oauth2/ajax/getLoginWay', data=data, headers=headers, timeout=(5,10))
print(res.json())
res = session.get(res.json()['loginInteractInfo']['cas']['casLoginRedirectUrl'], timeout=(5,10))
data = re.findall(r"loginCallback\?(.*)$", res.url)[0]
res = session.post('https://oauth-login.cloud.huawei.com/oauth2/ajax/login', data=data, headers=headers, timeout=(5,10))
res = session.get(res.json()['code'], timeout=(5,10))
headers = {'Content-Type': 'application/json'}
res = session.post('https://cloud.huawei.com/mobile/locate', headers=headers, data=json.dumps(deviceParam), timeout=(5,10))
assert res.text[:1] == '{'
print(res.json())
res = session.post('https://cloud.huawei.com/mobile/queryLocateResult', headers=headers, data=json.dumps(deviceParam), timeout=(5,10))
assert res.text[:1] == '{'
mobileInfo = res.json()
print(mobileInfo)
assert mobileInfo['resultCode'] == '0'
session.cookies.save(ignore_discard=True)
for k0 in mobileInfo.keys():
if type(mobileInfo[k0]).__name__ == 'str' and mobileInfo[k0][:1] == '{':
mobileInfo[k0] = json.loads(mobileInfo[k0])
for k1 in mobileInfo[k0].keys():
if type(mobileInfo[k0][k1]).__name__ == 'str' and mobileInfo[k0][k1][:1] == '{':
mobileInfo[k0][k1] = json.loads(mobileInfo[k0][k1])
print(json.dumps(mobileInfo, sort_keys=True))
lat = mobileInfo['info']['latitude_WGS']
lng = mobileInfo['info']['longitude_WGS']
print(lat, lng)
except requests.exceptions.RequestException:
print('Network timeout')
except Exception as e:
print(e)
print(traceback.format_exc())
get_location()
@Konano
Copy link
Author

Konano commented Oct 8, 2021

PS: 登陆的 Cookies 有效期是 10 年

@Konano
Copy link
Author

Konano commented Nov 10, 2022

MOBILE 的参数需要自行填入,具体值可自行与 https://cloud.huawei.com/ 交互并抓包得到。

@Konano
Copy link
Author

Konano commented Nov 10, 2022

MOBILE 的参数需要自行填入,具体值可自行与 https://cloud.huawei.com/ 交互并抓包得到。

deviceid hwid_cas_sid 这两个参数是在哪里得到的呢?

自己登录 https://cloud.huawei.com/ 然后使用「查找我的设备」的功能,全过程开 F12 监控请求,在里面找这两个参数就可以了。

@Konano
Copy link
Author

Konano commented Nov 10, 2022

据反馈,hwid_cas_sid 不是必选项。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment