From 10f6bac3c949120dcdeefdee2890f5937d432bd6 Mon Sep 17 00:00:00 2001 From: Alexis Pentori Date: Mon, 25 Mar 2024 15:13:44 +0100 Subject: [PATCH] twitter: add script to generate Twitter API token Signed-off-by: Alexis Pentori --- twitter/oauth.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 twitter/oauth.py diff --git a/twitter/oauth.py b/twitter/oauth.py new file mode 100755 index 0000000..60306fc --- /dev/null +++ b/twitter/oauth.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +import tweepy +import os + +""" +Script to generate token to allow twitter dev account to +use API as another twitter account. + +Require tweepy dependency +Use for airbyte connector +""" + +def print_help(): + print("The variables CLIENT_ID, CLIENT_SECRET and REDIRECT_URI must be set") + print("The CLIENT_ID and CLIENT_SECRET are generated when creating a twitter App : https://developer.twitter.com/en/portal/projects-and-apps") + print("The REDIRECT_URI is set throught the User Authentication settings in the twitter app and must be in https") + +if "CLIENT_ID" not in os.environ: + print_help() + exit(1) +if "CLIENT_SECRET" not in os.environ: + print_help() + exit(1) +if "REDIRECT_URI" not in os.environ: + print_help() + exit(1) + +oauth2_user_handler = tweepy.OAuth2UserHandler( + client_id= os.environ.get("CLIENT_ID"), + redirect_uri=os.environ.get("REDIRECT_URI"), + scope=["tweet.read", "users.read", "tweet.write", "offline.access"], + client_secret=os.environ.get("CLIENT_SECRET") +) + +print("Copy this url in a browser logged in the Twitter account.") +print(oauth2_user_handler.get_authorization_url()) + +authorization_response_url=input("Enter the authorization response URL. ") + +print("URL : %s" % authorization_response_url) + +access_token = oauth2_user_handler.fetch_token( + authorization_response_url +) + +print("Access token : %s" % access_token)