-
Notifications
You must be signed in to change notification settings - Fork 44
/
friends.py
79 lines (63 loc) · 2.08 KB
/
friends.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
68
69
70
71
72
73
74
75
76
77
78
79
"""
Example script that enables using your one-time-free gamertag change
"""
import argparse
import asyncio
import os
from pprint import pprint
import sys
from httpx import HTTPStatusError
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.signed_session import SignedSession
from xbox.webapi.scripts import CLIENT_ID, CLIENT_SECRET, TOKENS_FILE
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 SignedSession() as session:
auth_mgr = AuthenticationManager(
session, args.client_id, args.client_secret, ""
)
with open(args.tokens) as f:
tokens = f.read()
auth_mgr.oauth = OAuth2TokenResponse.model_validate_json(tokens)
try:
await auth_mgr.refresh_tokens()
except HTTPStatusError:
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 HTTPStatusError:
print("Invalid HTTP response")
sys.exit(-1)
pprint(resp.dict())
def main():
asyncio.run(async_main())
if __name__ == "__main__":
main()