I am trying to build a simple upload application for personal use. The reason why I am posting is the google documentation and the stack overflow posts on this subject are rather confusing for a person new to google API's. Ive already went through the google cloud setup involving creating a secret key and a client id (client_secret.json)
My problem is that I want to be able to run through the o-auth2 authentication flow to gain access to the API without opening a browser. This is because the script will live on a headless Ubuntu server once complete.
Ive already read some posts about refresh tokens and access tokens in the follow posts
YouTube Data API v3: video upload from server without opening the browser
How to refresh an access token for the youtube-data-api v3
Ive also read some google documentation
https://developers.google.com/youtube/v3/guides/authentication#OAuth2_Refreshing_a_Token
https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps
These articles helped me understand whats going on but they are not actionable as it seems like the google documentation is fragmented/out of date.
This is my code that comes from the get started guide. I only had to change 1 function to get it to run.
import osimport google_auth_oauthlib.flowimport googleapiclient.discoveryimport googleapiclient.errorsscopes = ["https://www.googleapis.com/auth/youtube.readonly"]def main(): # Disable OAuthlib's HTTPS verification when running locally. # *DO NOT* leave this option enabled in production. os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" api_service_name = "youtube" api_version = "v3" client_secrets_file = "client_secret.json" # Get credentials and create an API client flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file( client_secrets_file, scopes) # Was flow.run_console but that method was deprecated so I found the answer at https://stackoverflow.com/questions/75602866/google-oauth-attributeerror-installedappflow-object-has-no-attribute-run-co credentials = flow.run_local_server(open_browser=False) youtube = googleapiclient.discovery.build( api_service_name, api_version, credentials=credentials) request = youtube.channels().list( part="snippet,contentDetails,statistics", mine=True ) response = request.execute() print(response)if __name__ == "__main__": main()The code runs but when I click the link generated in the output I get a redirect error but that's not important to overall issue of being unable to authenticate without a browser.