forked from OpenXbox/xbox-webapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme_example.py
67 lines (58 loc) · 2.25 KB
/
readme_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# this is just script from README.md
import sys
import asyncio
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
from xbox.webapi.common.exceptions import AuthenticationException
from xbox import *
client_id = 'YOUR CLIENT ID HERE'
client_secret = 'YOUR CLIENT SECRET HERE'
"""
For doing authentication, see xbox/webapi/scripts/authenticate.py
"""
async def async_main():
tokens_file = "./tokens.json" # replace with path in auth scrip or just paste file with tokens here
async with ClientSession() as session:
auth_mgr = AuthenticationManager(
session, client_id, client_secret, ""
)
try:
with open(tokens_file, mode="r") as f:
tokens = f.read()
auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens)
except FileNotFoundError:
print(f'File {tokens_file} isn`t found or it doesn`t contain tokens!')
exit(-1)
try:
await auth_mgr.refresh_tokens()
except ClientResponseError:
print("Could not refresh tokens")
sys.exit(-1)
with open(tokens_file, mode="w") as f:
f.write(auth_mgr.oauth.json())
print(f'Refreshed tokens in {tokens_file}!')
xbl_client = XboxLiveClient(auth_mgr)
# Some example API calls
# Get friendslist
friendslist = await xbl_client.people.get_friends_own()
print('Your friends:')
print(friendslist)
print()
# Get presence status (by list of XUID)
presence = await xbl_client.presence.get_presence_batch(["2533274794093122", "2533274807551369"])
print('Statuses of some random players by XUID:')
print(presence)
print()
# Get messages
messages = await xbl_client.message.get_inbox()
print('Your messages:')
print(messages)
print()
# Get profile by GT
profile = await xbl_client.profile.get_profile_by_gamertag("SomeGamertag")
print('Profile under SomeGamertag gamer tag:')
print(profile)
print()
asyncio.run(async_main())