Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Youtube API token expires after a few days

$
0
0

I wrote a little python app which will stop and create broadcast via the Youtube API at defined times.

It works all fine but the token created at the first start or when a token does not exists when the app is started gets expired after a few days.

A refresh token is included but after a few days it fails to refresh the token.

I would like to only have to login once and just allow a refresh of the token.

Here is my login function

SCOPES = ["https://www.googleapis.com/auth/youtube"]......    def _authenticate(self, create_new_token: bool = False):        try:            credentials = None            dirname = os.path.dirname(__file__)            token_filename = os.path.join(dirname, self.TOKEN_FILE)            if os.path.exists(token_filename):  # do we have a token file already?                credentials = Credentials.from_authorized_user_file(                    token_filename, self.SCOPES                )            if create_new_token:                credentials = None  # will force to have to login again and create a new token file            if (                not credentials or not credentials.valid            ):  # do we have no token or is the token no longer valid?                if (                    credentials and credentials.expired and credentials.refresh_token                ):  # is the token expired?                    try:                        self.logger.debug("Token expired. Attempting to refresh...")                        credentials.refresh(Request())                        self.logger.debug("Token successfully refreshed.")                    except Exception as e:                        self.logger.error(f"Error renewing token: {e}")                        raise e                else:                    try:                        dirname = os.path.dirname(__file__)                        filename = os.path.join(                            dirname,                            self.config[self.CONF_YOUTUBE_SETTINGS][                                self.CONF_CREDENTIALS_FILE                            ],                        )                        flow = InstalledAppFlow.from_client_secrets_file(                            filename,                            self.SCOPES,                        )                        credentials = flow.run_local_server(                            port=0, access_type="offline", prompt="consent"                        )                    except Exception as e:                        self.logger.error(                            f"Failed to login, maybe the credentials file does not exist? `{filename}` Error was: {e}"                        )                        raise e                try:                    with open(token_filename, "w") as token:                        token.write(credentials.to_json())                        self.logger.info(                            f"Token was successfully written to token file `{token_filename}`"                        )                except Exception as e:                    self.logger.error(                        f"Failed to login, maybe the credentials file does not exist? `{token_filename}` Error was: {e}"                    )                    raise e            self.youtube = build("youtube", "v3", credentials=credentials)            return self.youtube        except Exception as e:            self.logger.error(f"Failed to authenticate: {str(e)}")            return None

Here are some logs of my python script:

Successful refresh of the token

2025-01-13 23:59:02,157 - DEBUG - Token expired. Attempting to refresh...2025-01-13 23:59:02,157 - DEBUG - Making request: POST https://oauth2.googleapis.com/token2025-01-13 23:59:02,159 - DEBUG - Starting new HTTPS connection (1): oauth2.googleapis.com:4432025-01-13 23:59:02,584 - DEBUG - https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None2025-01-13 23:59:02,584 - DEBUG - Token successfully refreshed.2025-01-13 23:59:02,585 - INFO - Token was successfully written to token file `/home/My/youtube-manager/token.secret`2025-01-13 23:59:02,587 - INFO - file_cache is only supported with oauth2client<4.0.02025-01-13 23:59:02,598 - DEBUG - URL being requested: POST https://youtube.googleapis.com/youtube/v3/liveBroadcasts/transition?part=status&broadcastStatus=complete&id=11111&alt=json2025-01-13 23:59:04,189 - INFO - Broadcast with  the id `11111` was stopped successfully

And here it failed

2025-01-14 11:59:02,161 - DEBUG - Token expired. Attempting to refresh...2025-01-14 11:59:02,162 - DEBUG - Making request: POST https://oauth2.googleapis.com/token2025-01-14 11:59:02,164 - DEBUG - Starting new HTTPS connection (1): oauth2.googleapis.com:4432025-01-14 11:59:02,398 - DEBUG - https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 400 None2025-01-14 11:59:02,399 - ERROR - Error renewing token: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})2025-01-14 11:59:02,399 - ERROR - Failed to authenticate: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})2025-01-14 11:59:02,399 - ERROR - Failed to stop broadcast: 'YouTubeStreamManager' object has no attribute 'youtube'

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>