I am working a Python based Docker Container running a YouTube-Discord Bot which uses the YouTube Analytics and Data API to fetch analytics data for content creators, however every couple of weeks the token / credentials.json expire causing the user to inconveniently have to re-authenticate/regenerate the token and rebuild a docker image and container to make the bot work again.
The following exception is thrown when credentials have expired and a method is called:Exception:
Traceback (most recent call last): File "c:\Users\Frost\Desktop\CodingProjects\youtube-analytics-bot\main.py", line 138, in get_stats response = execute_api_request( File "c:\Users\Frost\Desktop\CodingProjects\youtube-analytics-bot\main.py", line 85, in execute_api_request return client_library_function(**kwargs).execute() File "C:\Python310\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) File "C:\Python310\lib\site-packages\googleapiclient\http.py", line 923, in execute resp, content = _retry_request( File "C:\Python310\lib\site-packages\googleapiclient\http.py", line 191, in _retry_request resp, content = http.request(uri, method, *args, **kwargs) File "C:\Python310\lib\site-packages\oauth2client\transport.py", line 186, in new_request credentials._refresh(orig_request_method) File "C:\Python310\lib\site-packages\oauth2client\client.py", line 761, in _refresh self._do_refresh_request(http) File "C:\Python310\lib\site-packages\oauth2client\client.py", line 819, in _do_refresh_request raise HttpAccessTokenRefreshError(error_msg, status=resp.status)oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Token has been expired or revoked.I need to implement some way to re-generate credentials.json without having to rebuild an entire docker image/container. Possibly by using The --noauth_local_webserver command line argument which would make the docker container prompt me to input a verification code. For this solution to work, I would need to figure out how to send input to the script while the docker container is running (cannot use subprocess and docker run command for this.)
Example output:
python main.py --noauth_local_webserver2023-02-09 23:23:02 INFO discord.client logging in using static token2023-02-09 23:23:03 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: XX).Updating dates to 2023-02-01 - 2023-02-09Go to the following link in your browser:https://accounts.google.com/o/oauth2/auth?client_id=XX-XX.apps.googleusercontent.com&redirect_uri=urn%X%X%X%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics-monetary.readonly&access_type=offline&response_type=codeEnter verification code: Tried creating a method to refresh the token but it failed and did not generate new credentials.json
# Create Refresh Token (Un-Tested & unused)def refresh_token(): flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) flow.run_local_server(port=0) credentials = flow.credentials storage = Storage('credentials.json') storage.put(credentials)