有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Microsoft REST API Java将XboxLive玩家代号转换为XUID

我有一个Java应用程序,它需要能够为Minecraft Basefick Edition获取用户输入的gamertag,并将其转换为给定帐户的XUID,这样我就可以将其存储起来,以便以后用于白名单和参考目的

我一直在浏览Microsoft REST API文档,寻找一种方法来实现这一点,但我能找到的最接近的东西是:

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

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

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


共 (1) 个答案

  1. # 1 楼答案

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

    它的灵感来源于Team OpenXboxXbox-Webapi抄袭/浓缩而来的,,你可能应该用它来代替*。他们的API非常好,涵盖了微软的很多奥秘;如果你的项目依赖微软的Xbox Live API作为其核心功能,那么就值得为这个库而强烈考虑将你的项目切换到Python

    简而言之,要使用此API,似乎必须:

    1. 注册一个Application in Azure

      • 如果您的应用程序可以绑定到将授权该应用程序的用户系统上的localhost:8080,或者您得到了他们的合作(特别是:他们能够从浏览器中的URL将code参数粘贴到程序中),您可以跳过这一步,使用client_id=0000000048093EE3,并完全忽略client_secret。(在这种情况下,您甚至不需要Azure帐户。)
    2. 任何**Xbox Live用户通过OAuth2向您的应用程序提供Xboxlive.signinXboxlive.offline_access作用域

    3. 使用此授权和承载令牌从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(其中包括属性"id"处的XUID,作为一个十进制字符串

    (**很明显,如果你访问的是特权端点,比如那些查看私人信息或修改用户帐户设置的端点,你会对需要谁的授权以及需要请求什么范围有额外的要求;但是,对于从gamertag到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)