通过用户名获取 ID(Roblox)

问题描述 投票:0回答:5

我想知道如何通过 ROBLOX 的用户 API 发送请求,但似乎没有具体记录。 我正在通过状态登录网站,我需要 ID 来获取用户状态。感谢您提供的任何帮助。


如果我绝对需要使用

/v1/user/search
,我想得到第一个用户的id。

javascript html api roblox
5个回答
3
投票

您只需发出一个获取请求并从 URL 中获取用户 ID。

function getUserID(name)
{
    return new Promise((res, rej) => {
        fetch(`https://www.roblox.com/users/profile?username=${name}`)
            .then(r => {
                // check to see if URL is invalid.
                if (!r.ok) { throw "Invalid response"; }
                // return the only digits in the URL "the User ID"
                return r.url.match(/\d+/)[0];
            })
            .then(id =>{
                // this is where you get your ID
                console.log(id);
            })
    })
}

// without Promise

function getUserID(name)
{
    fetch(`https://www.roblox.com/users/profile?username=${name}`)
        .then(r => {
            if (!r.ok) { throw "Invalid response"; }
            return r.url.match(/\d+/)[0];
        })
        .then(id => {
            console.log(id);
        })
}

抱歉,我不熟悉在 StackOverflow 上发布答案。如果您需要任何帮助,请随时询问。


1
投票

只需向

https://api.roblox.com/users/get-by-username?username=UserName
发送一个获取请求,如果你想在此处使用js,它非常简单。



var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};

fetch("https://api.roblox.com/users/get-by-username?username=xLeki", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));```

0
投票

您可以使用

Players:GetUserIdFromNameAsync()
或我找到的这个DevForm链接

这些可能不正确,因为游戏网站对我来说被屏蔽了:(


0
投票

要获取用户名,您可以使用

https://users.roblox.com/v1/users/<USER ID>
,同时获取用户的状态,您需要
https://users.roblox.com/v1/users/<USER ID>/status
.


0
投票

导入请求 导入 json

def get_user_id(用户名):

url = 'https://users.roblox.com/v1/usernames/users'

# Request body as a JSON string
request_body = {
    'usernames': [username],
    'excludeBannedUsers': True
}
json_data = json.dumps(request_body)


headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}
response = requests.post(url, headers=headers, data=json_data)


user_data = json.loads(response.text)
if len(user_data['data']) > 0:
    user_id = user_data['data'][0]['id']
    return user_id
else:
    return None

用户名 = 'lilboii36' user_id = get_user_id(用户名) 如果 user_id: print(f"{username} 的用户 ID: {user_id}") 别的: print(f"没有找到用户名为{username}的用户")

© www.soinside.com 2019 - 2024. All rights reserved.