async def on_startup(self): await Tortoise.init(db_url=os.environ.get("DB_URL"), modules={"models": ["common.models"]}) self.redis = aioredis.from_url(os.environ.get("REDIS_URL"), decode_responses=True) self.session = aiohttp.ClientSession() auth_mgr = AuthenticationManager( self.session, os.environ["XBOX_CLIENT_ID"], os.environ["XBOX_CLIENT_SECRET"], "", ) auth_mgr.oauth = OAuth2TokenResponse.parse_file( os.environ["XAPI_TOKENS_LOCATION"]) await auth_mgr.refresh_tokens() xbl_client = XboxLiveClient(auth_mgr) self.profile = ProfileProvider(xbl_client) self.club = ClubProvider(xbl_client) self.realms = RealmsAPI(aiohttp.ClientSession()) headers = { "X-Authorization": os.environ["OPENXBL_KEY"], "Accept": "application/json", "Accept-Language": "en-US", } self.openxbl_session = aiohttp.ClientSession(headers=headers)
async def test(): async with ClientSession() as session: auth_mgr = AuthenticationManager(session, args.client_id, args.client_secret, "") with open(args.tokens, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) try: auth_mgr.refresh_tokens() except ClientResponseError: print("Could not refresh tokens") sys.exit(-1) with open(args.tokens, mode="w") as f: f.write(auth_mgr.oauth.json()) xbl_client = XboxLiveClient(auth_mgr) # Some example API calls # Get friendslist friendslist = await xbl_client.people.get_friends_own() # Get presence status (by list of XUID) presence = await xbl_client.presence.get_presence_batch( ["12344567687845", "453486346235151"]) # Get messages messages = await xbl_client.message.get_inbox() # Get profile by GT profile = await xbl_client.profile.get_profile_by_gamertag( "SomeGamertag")
async def auth_mgr(event_loop): session = ClientSession(loop=event_loop) mgr = AuthenticationManager(session, "abc", "123", "http://localhost") mgr.oauth = OAuth2TokenResponse.parse_raw(get_response("auth_oauth2_token")) mgr.user_token = XAUResponse.parse_raw(get_response("auth_user_token")) mgr.xsts_token = XSTSResponse.parse_raw(get_response("auth_xsts_token")) yield mgr await session.close()
async def auth_mgr(event_loop): mgr = AuthenticationManager( ClientSession(loop=event_loop), "abc", "123", "http://localhost" ) mgr.oauth = OAuth2TokenResponse.parse_raw(get_response("auth_oauth2_token")) mgr.user_token = XAUResponse.parse_raw(get_response("auth_user_token")) mgr.xsts_token = XSTSResponse.parse_raw(get_response("auth_xsts_token")) return mgr
async def async_main(): parser = argparse.ArgumentParser(description="Change your gamertag") parser.add_argument( "--tokens", "-t", default=TOKENS_FILE, help=f"Token filepath. Default: '{TOKENS_FILE}'", ) parser.add_argument( "--client-id", "-cid", default=os.environ.get("CLIENT_ID", CLIENT_ID), help="OAuth2 Client ID", ) parser.add_argument( "--client-secret", "-cs", default=os.environ.get("CLIENT_SECRET", CLIENT_SECRET), help="OAuth2 Client Secret", ) args = parser.parse_args() if not os.path.exists(args.tokens): print("No token file found, run xbox-authenticate") sys.exit(-1) async with ClientSession() as session: auth_mgr = AuthenticationManager( session, args.client_id, args.client_secret, "" ) with open(args.tokens, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) try: await auth_mgr.refresh_tokens() except ClientResponseError: print("Could not refresh tokens") sys.exit(-1) with open(args.tokens, mode="w") as f: f.write(auth_mgr.oauth.json()) xbl_client = XboxLiveClient(auth_mgr) try: resp = await xbl_client.people.get_friends_own() except ClientResponseError: print("Invalid HTTP response") sys.exit(-1) pprint(resp.dict())
async def async_main(client_id: str, client_secret: str, redirect_uri: str, token_filepath: str): async with ClientSession() as session: auth_mgr = AuthenticationManager(session, client_id, client_secret, redirect_uri) # Refresh tokens if we have them if os.path.exists(token_filepath): with open(token_filepath, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) await auth_mgr.refresh_tokens() # Request new ones if they are not valid if not (auth_mgr.xsts_token and auth_mgr.xsts_token.is_valid()): auth_url = auth_mgr.generate_authorization_url() webbrowser.open(auth_url) code = await queue.get() await auth_mgr.request_tokens(code) with open(token_filepath, mode="w") as f: f.write(auth_mgr.oauth.json())
async def do_authentication(args: argparse.Namespace) -> AuthenticationManager: """ Shortcut for doing xbox live authentication (uses xbox-webapi-python lib). Args: args: Parsed arguments Returns: An authenticated instance of AuthenticationManager Raises: AuthenticationException: If authentication failed """ async with aiohttp.ClientSession() as session: auth_mgr = AuthenticationManager( session, args.client_id, args.client_secret, args.redirect_uri ) # Refresh tokens if we have them if os.path.exists(args.tokens): with open(args.tokens, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) await auth_mgr.refresh_tokens() # Request new ones if they are not valid if not (auth_mgr.xsts_token and auth_mgr.xsts_token.is_valid()): auth_url = auth_mgr.generate_authorization_url() print(f'Authorize with following URL: {auth_url}\n') code = input('Enter received authorization code: ') await auth_mgr.request_tokens(code) with open(args.tokens, mode="w") as f: f.write(auth_mgr.oauth.json()) return auth_mgr
async def async_main(): parser = argparse.ArgumentParser(description="Change your gamertag") parser.add_argument( "--tokens", "-t", default=TOKENS_FILE, help=f"Token filepath. Default: '{TOKENS_FILE}'", ) parser.add_argument( "--client-id", "-cid", default=os.environ.get("CLIENT_ID", CLIENT_ID), help="OAuth2 Client ID", ) parser.add_argument( "--client-secret", "-cs", default=os.environ.get("CLIENT_SECRET", CLIENT_SECRET), help="OAuth2 Client Secret", ) parser.add_argument("gamertag", help="Desired Gamertag") args = parser.parse_args() if len(args.gamertag) > 15: print("Desired gamertag exceedes limit of 15 chars") sys.exit(-1) if not os.path.exists(args.tokens): print("No token file found, run xbox-authenticate") sys.exit(-1) async with ClientSession() as session: auth_mgr = AuthenticationManager(session, args.client_id, args.client_secret, "") with open(args.tokens, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) try: await auth_mgr.refresh_tokens() except ClientResponseError: print("Could not refresh tokens") sys.exit(-1) with open(args.tokens, mode="w") as f: f.write(auth_mgr.oauth.json()) xbl_client = XboxLiveClient(auth_mgr) print(":: Trying to change gamertag to '%s' for xuid '%i'..." % (args.gamertag, xbl_client.xuid)) print("Claiming gamertag...") try: resp = await xbl_client.account.claim_gamertag( xbl_client.xuid, args.gamertag) if resp == ClaimGamertagResult.NotAvailable: print( "Claiming gamertag failed - Desired gamertag is unavailable" ) sys.exit(-1) except ClientResponseError: print("Invalid HTTP response from claim") sys.exit(-1) print("Changing gamertag...") try: resp = await xbl_client.account.change_gamertag( xbl_client.xuid, args.gamertag) if resp == ChangeGamertagResult.NoFreeChangesAvailable: print("Changing gamertag failed - You are out of free changes") sys.exit(-1) except ClientResponseError: print("Invalid HTTP response from change") sys.exit(-1) print("Gamertag successfully changed to %s" % args.gamertag)
async def async_main(): parser = argparse.ArgumentParser(description="Change your gamertag") parser.add_argument( "--tokens", "-t", default=TOKENS_FILE, help=f"Token filepath. Default: '{TOKENS_FILE}'", ) parser.add_argument( "--client-id", "-cid", default=os.environ.get("CLIENT_ID", CLIENT_ID), help="OAuth2 Client ID", ) parser.add_argument( "--client-secret", "-cs", default=os.environ.get("CLIENT_SECRET", CLIENT_SECRET), help="OAuth2 Client Secret", ) args = parser.parse_args() if not os.path.exists(args.tokens): print("No token file found, run xbox-authenticate") sys.exit(-1) async with ClientSession() as session: auth_mgr = AuthenticationManager( session, args.client_id, args.client_secret, "" ) with open(args.tokens, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) try: await auth_mgr.refresh_tokens() except ClientResponseError: print("Could not refresh tokens") sys.exit(-1) with open(args.tokens, mode="w") as f: f.write(auth_mgr.oauth.json()) xbl_client = XboxLiveClient(auth_mgr) try: uniname = "c:/users/willr/downloads/UNIQUENAME9.mp4" # resp = await xbl_client.screenshots.get_recent_own_screenshots(max_items=3) resp = await xbl_client.gameclips.get_recent_own_clips(max_items=1) # downloadLink = resp.game_clips[0].game_clip_uris[0].uri # wget.download(downloadLink, uniname) # clip = VideoFileClip(uniname).subclip(8) # clip.write_videofile("c:/users/willr/downloads/UNIQUENAME9-finished.mp4",temp_audiofile='temp-audio.m4a', remove_temp=True, codec="libx264", audio_codec="aac") # clip.close() # highlightz = TweetMachine() # highlightz.makeAVidTweet('c:/users/willr/downloads/UNIQUENAME9-finished.mp4','🚨🚨🚨BUZZER BEATER ALERT🚨🚨🚨') except ClientResponseError: print("Invalid HTTP response") sys.exit(-1) pprint(resp.dict())