使用 Microsoft REST API - Java 将 Xbox-Live GamerTag 转换为 XUID

Cry*_*bub 5 java xbox-live

我有一个 Java 应用程序,它需要能够获取用户输入的 Minecraft-Bedrock Edition 玩家标签,并将其转换为给定帐户的 XUID,以便我可以将其存储起来以供稍后列入白名单和参考目的。

我一直在浏览 Microsoft REST API 文档,寻找一种可以让我执行此操作的方法,但我能找到的最接近的方法是:

https://learn.microsoft.com/en-us/gaming/xbox-live/xbox-live-rest/uri/profilev2/uri-usersbatchprofilesettingspost

它仍然需要 XUID 作为输入,而不是提供它作为输出。

有什么方法可以将玩家代号的给定字符串输入转换为关联帐户的 XUID 或 null(如果 Java 应用程序不存在此类帐户)?

Jam*_*ude 5

我已经用纯粹的、独立的bash+curl+sed.

\n

它很大程度上受到来自OpenXbox 团队的Xbox-Webapixbox.webapi.authentication.manager模块的抄袭/压缩的启发,您可能应该使用它来代替*。他们的 API 非常好,涵盖了如此多的奥秘,以至于 Microsoft\xe2\x80\xa6根本无法记录;如果您的项目依赖于 Microsoft 的 Xbox Live API 来实现其核心功能,则值得强烈考虑将您的项目仅针对此库切换到 Python。

\n

简而言之,要使用这个 API,看起来:

\n
    \n
  1. 在 Azure 中注册应用程序

    \n
      \n
    • 如果您的应用程序可以绑定到localhost:8080要授权该应用程序的用户的系统,或者您以其他方式获得他们的合作(具体来说:他们能够code从浏览器中的 URL 将参数粘贴到程序中) ,你可以跳过这一步,使用client_id=0000000048093EE3,完全省略client_secret。(在这种情况下,你甚至不需要 Azure 帐户。)
    • \n
    \n
  2. \n
  3. 让任何** Xbox Live 用户提供Xboxlive.signin和Xboxlive.offline_access** Xbox Live 用户通过 OAuth2 向您的应用程序

    \n
  4. \n
  5. 使用此授权和不记名令牌来获取所谓的“用户令牌”https://user.auth.xboxlive.com/user/authenticate

    \n
  6. \n
  7. 使用该令牌对自己进行身份验证以https://xsts.auth.xboxlive.com/xsts/authorize获得“ XToken ”

    \n
  8. \n
  9. 使用该令牌对https://profile.xboxlive.com您感兴趣的实际端点进行身份验证,例如/users/gt({gamertag})/profile/settings(其中包含 XUID,作为十进制字符串,位于属性"id")

    \n
  10. \n
\n

(**显然,如果你正在使用特权端点,例如查看私人信息或修改用户帐户设置的端点,您将对您需要的授权以及您必须请求的范围有额外的要求;但是,对于玩家代号到 XUID 查找的一般情况,只需任何用户登录即可。)

\n
\n

*为此,它会是这样的:

\n
from asyncio import run as arun\nfrom sys import argv\nfrom os import path, environ\nimport subprocess\nfrom aiohttp import ClientSession\nfrom xbox.webapi.api.client import XboxLiveClient\nfrom xbox.webapi.authentication.manager import AuthenticationManager\nfrom xbox.webapi.authentication.models import OAuth2TokenResponse\n\nasync def gamertags_to_xuids(gamertags, client_id=environ["client_id"], client_secret=environ["client_secret"], token_jar=path.join(environ["HOME"], ".local", "share", "xbox", "tokens.json")):\n  assert len(gamertags) >= 1\n  global session\n  auth_mgr = AuthenticationManager(session, client_id, client_secret, "")\n  await freshen_tokens(auth_mgr, token_jar)\n  xbl_client = XboxLiveClient(auth_mgr)\n\n  ids = []\n  for gamertag in gamertags:\n    profile = await xbl_client.profile.get_profile_by_gamertag(gamertag)\n    ids.append(int(profile.profile_users[0].id))\n  xuids = [f"{id:016X}" for id in ids]\n\n  return xuids\n\nasync def freshen_tokens(auth_mgr, token_jar):\n  with open(token_jar, "r+") as f:\n    auth_mgr.oauth = OAuth2TokenResponse.parse_raw(f.read())\n    await auth_mgr.refresh_tokens()\n    f.seek(0)\n    f.truncate()\n    f.write(auth_mgr.oauth.json())\n\nasync def amain(args):\n  global session\n  subprocess.run(["xbox-authenticate"], check=True)#TODO: avoid this system call\n  async with ClientSession() as session:\n    return await gamertags_to_xuids(args)\n\ndef main(args=argv[1:]):\n  return arun(amain(args))\n\nif __name__ == \'__main__\':\n  for xuid in main():\n    print(xuid)\n
Run Code Online (Sandbox Code Playgroud)\n