From 16dda277c88123cc676b54a15d2adb9afbce2c2a Mon Sep 17 00:00:00 2001 From: Ang Weoy Yang Date: Tue, 6 May 2025 15:39:44 +0800 Subject: [PATCH 1/2] feat(twitter_plugin): add an auth gate during initialization --- .../twitter_plugin_gamesdk/twitter_plugin.py | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/plugins/twitter/twitter_plugin_gamesdk/twitter_plugin.py b/plugins/twitter/twitter_plugin_gamesdk/twitter_plugin.py index 51416084..dbeed65c 100644 --- a/plugins/twitter/twitter_plugin_gamesdk/twitter_plugin.py +++ b/plugins/twitter/twitter_plugin_gamesdk/twitter_plugin.py @@ -61,6 +61,20 @@ def __init__(self, options: Dict[str, Any]) -> None: credentials = options.get("credentials") if not credentials: raise ValueError("Twitter API credentials are required.") + + # Capture token for internal use + self.game_twitter_access_token = credentials.get("gameTwitterAccessToken") + + # Auth gate: require EITHER gameTwitterAccessToken OR full credential set + has_api_credentials = all( + credentials.get(key) for key in ["bearerToken", "apiKey", "apiSecretKey", "accessToken", "accessTokenSecret"] + ) + + if not self.game_twitter_access_token and not has_api_credentials: + raise ValueError( + "Missing valid authentication. Provide either a 'gameTwitterAccessToken' or all required Twitter API credentials." + ) + # Init Tweepy client self.twitter_client: virtuals_tweepy.Client = virtuals_tweepy.Client( bearer_token = credentials.get("bearerToken"), @@ -74,7 +88,16 @@ def __init__(self, options: Dict[str, Any]) -> None: # Configure logging logging.basicConfig(level=logging.INFO) self.logger: logging.Logger = logging.getLogger(__name__) - - self.game_twitter_access_token = credentials.get("gameTwitterAccessToken") - + self._check_authentication() + + def _check_authentication(self) -> None: + """ + Check if the credentials provided are valid by calling the /me endpoint or fetching user info. + """ + try: + user = self.twitter_client.get_me(user_fields=["public_metrics"]).get('data') + self.logger.info(f"Authenticated as: {user.get('name')} (@{user.get('username')})") + except virtuals_tweepy.TweepyException as e: + self.logger.error(f"Authentication failed: {e}") + raise ValueError("Invalid Twitter credentials or failed authentication.") From 8424e4f6a9ae00a8b593b20aa44e85d7cd404d06 Mon Sep 17 00:00:00 2001 From: Ang Weoy Yang Date: Tue, 6 May 2025 15:39:57 +0800 Subject: [PATCH 2/2] docs: add .env.example to refer --- plugins/twitter/examples/.env.example | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 plugins/twitter/examples/.env.example diff --git a/plugins/twitter/examples/.env.example b/plugins/twitter/examples/.env.example new file mode 100644 index 00000000..80d8db35 --- /dev/null +++ b/plugins/twitter/examples/.env.example @@ -0,0 +1,7 @@ +GAME_TWITTER_ACCESS_TOKEN=apx- + +TWITTER_BEARER_TOKEN= +TWITTER_API_KEY= +TWITTER_API_SECRET_KEY= +TWITTER_ACCESS_TOKEN= +TWITTER_ACCESS_TOKEN_SECRET=