Skip to content

Instantly share code, notes, and snippets.

@ankush1411
Forked from CoreyMSchafer/YouTube-OAuth-Snippets
Created August 19, 2021 06:36
Show Gist options
  • Select an option

  • Save ankush1411/c4d22c5c0ef0c874f0a815e86d8abca6 to your computer and use it in GitHub Desktop.

Select an option

Save ankush1411/c4d22c5c0ef0c874f0a815e86d8abca6 to your computer and use it in GitHub Desktop.

Revisions

  1. @CoreyMSchafer CoreyMSchafer revised this gist Sep 10, 2020. No changes.
  2. @CoreyMSchafer CoreyMSchafer created this gist Sep 10, 2020.
    33 changes: 33 additions & 0 deletions YouTube-OAuth-Snippets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # token.pickle stores the user's credentials from previously successful logins
    if os.path.exists('token.pickle'):
    print('Loading Credentials From File...')
    with open('token.pickle', 'rb') as token:
    credentials = pickle.load(token)


    # Google's Request
    from google.auth.transport.requests import Request


    # If there are no valid credentials available, then either refresh the token or log in.
    if not credentials or not credentials.valid:
    if credentials and credentials.expired and credentials.refresh_token:
    print('Refreshing Access Token...')
    credentials.refresh(Request())
    else:
    print('Fetching New Tokens...')
    flow = InstalledAppFlow.from_client_secrets_file(
    'client_secrets.json',
    scopes=[
    'https://www.googleapis.com/auth/youtube.readonly'
    ]
    )

    flow.run_local_server(port=8080, prompt='consent',
    authorization_prompt_message='')
    credentials = flow.credentials

    # Save the credentials for the next run
    with open('token.pickle', 'wb') as f:
    print('Saving Credentials for Future Use...')
    pickle.dump(credentials, f)