74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
import asyncio
|
|
import json
|
|
import os
|
|
import webbrowser
|
|
from twitchAPI.twitch import Twitch
|
|
from twitchAPI.oauth import UserAuthenticator
|
|
from twitchAPI.type import AuthScope
|
|
|
|
SECRETS_FILE = "twitch_secrets.json"
|
|
|
|
def load_credentials():
|
|
"""Loads existing Client ID and Secret from your JSON file."""
|
|
if not os.path.exists(SECRETS_FILE):
|
|
raise FileNotFoundError(f"Could not find {SECRETS_FILE} in this directory.")
|
|
with open(SECRETS_FILE, "r") as f:
|
|
data = json.load(f)
|
|
return data.get("client_id"), data.get("client_secret")
|
|
|
|
def save_token_to_json(token):
|
|
"""Saves the generated token into twitch_secrets.json under 'manual_token'."""
|
|
with open(SECRETS_FILE, "r") as f:
|
|
data = json.load(f)
|
|
|
|
# Inject the new token
|
|
data["manual_token"] = token
|
|
|
|
with open(SECRETS_FILE, "w") as f:
|
|
json.dump(data, f, indent=4)
|
|
print(f"\n[SUCCESS] Token saved inside '{SECRETS_FILE}' under 'manual_token'!")
|
|
|
|
async def main():
|
|
try:
|
|
client_id, client_secret = load_credentials()
|
|
if not client_id or not client_secret:
|
|
print("[ERROR] Please add your client_id and client_secret to the JSON file first.")
|
|
return
|
|
|
|
print("Initializing local connection loop...")
|
|
# Initialize official Twitch connection interface
|
|
twitch = await Twitch(client_id, client_secret)
|
|
|
|
# Scopes: We leave this empty [] since VOD collection only requires basic public clearance
|
|
scopes = []
|
|
|
|
# Create an authenticator that automatically sets up http://localhost:17563
|
|
auth = UserAuthenticator(twitch, scopes, url="http://localhost:17563")
|
|
|
|
# Request authentication URL
|
|
auth_url = auth.return_auth_url()
|
|
print(f"\nIf your browser does not open automatically, copy and paste this URL into your browser:\n{auth_url}\n")
|
|
|
|
# Open your system default browser to let you manually click "Authorize"
|
|
webbrowser.open(auth_url)
|
|
|
|
print("Waiting for you to click 'Authorize' in your web browser...")
|
|
# The script halts here, running a local background server until you click authorize
|
|
token, refresh_token = await auth.authenticate()
|
|
|
|
print(f"\nSuccessfully generated Token: {token}")
|
|
|
|
# Save it right back into your configuration file
|
|
save_token_to_json(token)
|
|
|
|
# Gracefully shut down the library connection
|
|
await twitch.close()
|
|
|
|
except Exception as e:
|
|
print(f"\n[ERROR] An error occurred: {e}")
|
|
print("Double-check that http://localhost:17563 is added to your Twitch Dev Console.")
|
|
|
|
if __name__ == "__main__":
|
|
# Run the asynchronous loop
|
|
asyncio.run(main()) |