最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - how to properly refresh access token for google pubsub_v1.SubscriberClient - Stack Overflow

programmeradmin3浏览0评论

In my fast api server, it subscribes to the google realtime developer notifications.

python version 3.12.7 "google-cloud-pubsub (>=2.29.0,<3.0.0)"

The subscriber can be successfully initialized as below:

            # self.credentials is a dict from google service account credential json file
            self.subscriber = pubsub_v1.SubscriberClient.from_service_account_info(
                self.credentials) 

and used like this:

            self._streaming_pull_future = self.subscriber.subscribe(
                self.subscription_path,
                callback=callback,
                flow_control=flow_control,
            )
            logger.info(f"Listening for messages on {self.subscription_path}")

            while self._running:
                try:
                    if self._streaming_pull_future.done():
                        break
                    await asyncio.sleep(1)
                except asyncio.CancelledError:
                    break

The subscriber client can initialized, successfully pulling and ack the messages. However, after about 30 minute, it throw out the error:

Error in notification worker: 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See . [reason: "ACCESS_TOKEN_EXPIRED"
domain: "googleapis"
metadata {
  key: "service"
  value: "pubsub.googleapis"
}
metadata {
  key: "method"
  value: "google.pubsub.v1.Subscriber.StreamingPull"
}
]

The error indicates the access token has timed out. I have several questions: Is there a way to initialize the subscriber client so that it can automatically refresh the token itself? If not, what is the good practice that developer explicitly refresh the token, pass it to the subscriber?

发布评论

评论列表(0)

  1. 暂无评论