|
| 1 | +""" |
| 2 | + This example demonstrates how to automatically (re)generate tokens for continuous OAuth. |
| 3 | + We store the Access Token in a seperate .env file to be used later. |
| 4 | +""" |
| 5 | + |
| 6 | +from pyyoutube import Client |
| 7 | +from json import loads, dumps |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +CLIENT_ID = "xxx" # Your app id |
| 12 | +CLIENT_SECRET = "xxx" # Your app secret |
| 13 | +CLIENT_SECRET_PATH = None # or your path/to/client_secret_web.json |
| 14 | + |
| 15 | +TOKEN_PERSISTENT_PATH = None # path/to/persistent_token_storage_location |
| 16 | + |
| 17 | +SCOPE = [ |
| 18 | + "https://www.googleapis.com/auth/youtube", |
| 19 | + "https://www.googleapis.com/auth/youtube.force-ssl", |
| 20 | + "https://www.googleapis.com/auth/userinfo.profile", |
| 21 | +] |
| 22 | + |
| 23 | +def do_refresh(): |
| 24 | + token_location = Path(TOKEN_PERSISTENT_PATH) |
| 25 | + |
| 26 | + # Read the persistent token data if it exists |
| 27 | + token_data = {} |
| 28 | + if token_location.exists(): |
| 29 | + token_data = loads(token_location.read_text()) |
| 30 | + |
| 31 | + |
| 32 | + cli = Client( |
| 33 | + client_id=CLIENT_ID, |
| 34 | + client_secret=CLIENT_SECRET, |
| 35 | + access_token=token_data.get("access_token"), |
| 36 | + refresh_token=token_data.get("refresh_token") |
| 37 | + ) |
| 38 | + # or if you want to use a web type client_secret.json |
| 39 | + # cli = Client( |
| 40 | + # client_secret_path=CLIENT_SECRET_PATH, |
| 41 | + # access_token=token_data.get("access_token"), |
| 42 | + # refresh_token=token_data.get("refresh_token") |
| 43 | + # ) |
| 44 | + |
| 45 | + # If no access token is provided, this is the same as oauth_flow.py |
| 46 | + if not cli._has_auth_credentials(): |
| 47 | + authorize_url, state = cli.get_authorize_url(scope=SCOPE) |
| 48 | + print(f"Click url to do authorize: {authorize_url}") |
| 49 | + |
| 50 | + response_uri = input("Input youtube redirect uri:\n") |
| 51 | + |
| 52 | + token = cli.generate_access_token(authorization_response=response_uri, scope=SCOPE) |
| 53 | + print(f"Your token: {token}") |
| 54 | + |
| 55 | + # Otherwise, refresh the access token if it has expired |
| 56 | + else: |
| 57 | + token = cli.refresh_access_token(cli.refresh_token) |
| 58 | + |
| 59 | + # we add the token data to the client and token objects so that they are complete |
| 60 | + token.refresh_token = cli.refresh_token |
| 61 | + cli.access_token = token.access_token |
| 62 | + print(f"Your token: {token}") |
| 63 | + |
| 64 | + # Write the token data to the persistent location to be used again, ensuring the file exists |
| 65 | + token_location.mkdir(parents=True, exist_ok=True) |
| 66 | + token_location.write_text( |
| 67 | + dumps( |
| 68 | + { |
| 69 | + "access_token": token.access_token, |
| 70 | + "refresh_token": token.refresh_token |
| 71 | + } |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + # Now you can do things with the client |
| 76 | + resp = cli.channels.list(mine=True) |
| 77 | + print(f"Your channel id: {resp.items[0].id}") |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + do_refresh() |
0 commit comments