【问题标题】:Convert Xbox-Live GamerTag to XUID using Microsoft REST API - Java使用 Microsoft REST API - Java 将 Xbox-Live GamerTag 转换为 XUID
【发布时间】:2020-02-17 01:03:12
【问题描述】:

我有一个 Java 应用程序,它需要能够为 Minecraft-Bedrock 版获取用户输入的玩家代号并将其转换为给定帐户的 XUID,以便我可以将其存储起来以供以后加入白名单和参考之用.

我一直在浏览 Microsoft REST API 文档,寻找一种可以让我这样做的方法,但我能找到的最接近的是:

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

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

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

【问题讨论】:

    标签: java xbox-live


    【解决方案1】:

    我已经用纯粹的、独立的bash+curl+sed 写了一个illustrative proof-of-concept implementation

    它在很大程度上受到来自Team OpenXboxXbox-Webapixbox.webapi.authentication.manager module 的剽窃/压缩的启发,您可能应该改用它*。他们的 API 非常好,涵盖了这么多奥秘,以至于微软…simply fails to document;如果该库依赖于 Microsoft 的 Xbox Live API 来实现其核心功能,那么值得强烈考虑将您的项目切换到 Python。

    简而言之,要使用此 API,看来您必须:

    1. 注册Application in Azure

      • 如果您的应用程序可以绑定到将要授权应用程序的用户系统上的localhost:8080,或者您有他们的合作(特别是:他们能够将code 参数粘贴到程序),您可以跳过此步骤,使用client_id=0000000048093EE3,并完全省略client_secret。 (在这种情况下,甚至不需要 Azure 帐户。)
    2. any** Xbox Live 用户通过 OAuth2 向您的应用程序提供 Xboxlive.signinXboxlive.offline_access 范围

    3. 使用此授权和 Bearer 令牌从 https://user.auth.xboxlive.com/user/authenticate 获取所谓的“用户令牌”

    4. 使用该令牌向https://xsts.auth.xboxlive.com/xsts/authorize 进行身份验证,以获得“XToken

    5. 使用该令牌向https://profile.xboxlive.com 验证您感兴趣的实际端点,例如/users/gt({gamertag})/profile/settings(其中包含XUID 等属性,作为十进制字符串 , 在属性"id")

    (**显然,如果您要访问 特权 端点,例如查看私人信息或修改用户帐户设置的端点,您将对您需要谁的授权以及您的范围有额外的要求' 必须请求;但是,对于一般情况下的 gamertag-to-XUID 查找,任何用户只需登录即可。)


    *为此,它会是这样的:

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

    【讨论】:

    • @CrypticCabub 如果您必须拥有此 API 访问权限,则没有人力或预算将该库移植或重新实现到 Java 中,但仍受限于使用 Java 作为代码库,你可以考虑Java-Python FFI
    猜你喜欢
    • 2013-12-23
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    相关资源
    最近更新 更多