@@ -56,75 +56,103 @@ Example: Search Xbox Live via cmdline tool
5656API usage
5757
5858``` py
59- import sys
6059import asyncio
60+ import sys
61+
6162from httpx import HTTPStatusError
62- from xbox.webapi.common.signed_session import SignedSession
63+
6364from xbox.webapi.api.client import XboxLiveClient
6465from xbox.webapi.authentication.manager import AuthenticationManager
6566from xbox.webapi.authentication.models import OAuth2TokenResponse
67+ from xbox.webapi.common.signed_session import SignedSession
6668from xbox.webapi.scripts import CLIENT_ID , CLIENT_SECRET , TOKENS_FILE
6769
68- # This uses the default client identification by OpenXbox
69- # Feel free to use your own here
70+ """
71+ This uses the global default client identification by OpenXbox
72+ You can supply your own parameters here if you are permitted to create
73+ new Microsoft OAuth Apps and know what you are doing
74+ """
7075client_id = CLIENT_ID
7176client_secret = CLIENT_SECRET
7277tokens_file = TOKENS_FILE
7378
7479"""
7580For doing authentication, see xbox/webapi/scripts/authenticate.py
7681"""
82+
83+
7784async def async_main ():
85+ # Create a HTTP client session
7886 async with SignedSession() as session:
79- auth_mgr = AuthenticationManager(
80- session, client_id, client_secret, " "
81- )
82-
87+ """
88+ Initialize with global OAUTH parameters from above
89+ """
90+ auth_mgr = AuthenticationManager(session, client_id, client_secret, " " )
91+
92+ """
93+ Read in tokens that you received from the `xbox-authenticate`-script previously
94+ See `xbox/webapi/scripts/authenticate.py`
95+ """
8396 try :
84- with open (tokens_file, mode = " r" ) as f:
85- tokens = f.read()
97+ with open (tokens_file) as f:
98+ tokens = f.read()
99+ # Assign gathered tokens
86100 auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens)
87101 except FileNotFoundError :
88- print (f ' File { tokens_file} isn`t found or it doesn`t contain tokens! ' )
102+ print (f " File { tokens_file} isn`t found or it doesn`t contain tokens! " )
89103 exit (- 1 )
90-
104+
105+ """
106+ Refresh tokens, just in case
107+ You could also manually check the token lifetimes and just refresh them
108+ if they are close to expiry
109+ """
91110 try :
92- await auth_mgr.refresh_tokens()
111+ await auth_mgr.refresh_tokens()
93112 except HTTPStatusError:
94- print (" Could not refresh tokens" )
95- sys.exit(- 1 )
113+ print (" Could not refresh tokens" )
114+ sys.exit(- 1 )
96115
116+ # Save the refreshed/updated tokens
97117 with open (tokens_file, mode = " w" ) as f:
98- f.write(auth_mgr.oauth.json())
99- print (f ' Refreshed tokens in { tokens_file} ! ' )
118+ f.write(auth_mgr.oauth.json())
119+ print (f " Refreshed tokens in { tokens_file} ! " )
100120
121+ """
122+ Construct the Xbox API client from AuthenticationManager instance
123+ """
101124 xbl_client = XboxLiveClient(auth_mgr)
102125
103- # Some example API calls
126+ """
127+ Some example API calls
128+ """
104129 # Get friendslist
105130 friendslist = await xbl_client.people.get_friends_own()
106- print (' Your friends:' )
131+ print (" Your friends:" )
107132 print (friendslist)
108133 print ()
109134
110135 # Get presence status (by list of XUID)
111- presence = await xbl_client.presence.get_presence_batch([" 2533274794093122" , " 2533274807551369" ])
112- print (' Statuses of some random players by XUID:' )
136+ presence = await xbl_client.presence.get_presence_batch(
137+ [" 2533274794093122" , " 2533274807551369" ]
138+ )
139+ print (" Statuses of some random players by XUID:" )
113140 print (presence)
114141 print ()
115142
116143 # Get messages
117144 messages = await xbl_client.message.get_inbox()
118- print (' Your messages:' )
145+ print (" Your messages:" )
119146 print (messages)
120147 print ()
121148
122149 # Get profile by GT
123150 profile = await xbl_client.profile.get_profile_by_gamertag(" SomeGamertag" )
124- print (' Profile under SomeGamertag gamer tag:' )
151+ print (" Profile under SomeGamertag gamer tag:" )
125152 print (profile)
126153 print ()
127154
155+
128156asyncio.run(async_main())
129157```
130158
0 commit comments