diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..b44c6b2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,28 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: + - id: check-executables-have-shebangs + - id: check-merge-conflict + - id: double-quote-string-fixer + - id: end-of-file-fixer + - id: fix-encoding-pragma + args: ['--remove'] + - id: requirements-txt-fixer + - id: trailing-whitespace + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.8.4 + hooks: + - id: flake8 + args: ['--max-line-length=120'] + - repo: https://github.com/FalconSocial/pre-commit-python-sorter + rev: master + hooks: + - id: python-import-sorter + args: ['--silent-overwrite'] + - repo: https://github.com/chewse/pre-commit-mirrors-pydocstyle + rev: v2.1.1 + hooks: + - id: pydocstyle + args: ['--config=.pydocstyle', '--match="(?!test_).*\.py"'] diff --git a/README.md b/README.md index 26c6888..27e7901 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,77 @@ # twitter-login -Grant a Twitter application to use your Twitter account on your behalf + +Grant a Twitter application to use your Twitter account on your behalf. + +## Description + +If you have a `CONSUMER_KEY` and a `CONSUMER_SECRET`, you would like to have an `ACCESS_TOKEN` and an +`ACCESS_TOKEN_SECRET`, you are at the right place! + +## Create consumer tokens + +Go to the [developper portal](https://developer.twitter.com/en/portal/dashboard) to create a Twitter application. Go to +"*Keys and tokens*", then "*Consumer keys*" and "*API key & secret*". Those keys are `CONSUMER_KEY` and +`CONSUMER_SECRET` settings. + +## Create access tokens + +Clone this repository: + +``` +git clone https://github.com/jouir/twitter-login.git +``` + +Setup the Python virtual environment: + +``` +sudo apt install python3-virtualenv +virtualenv venv +source venv/bin/activate +``` + +Install requirements: + +``` +pip install -r requirements.txt +``` + +Read consumer keys: + +``` +read -s CONSUMER_KEY +read -s CONSUMER_SECRET +``` + +Execute the script: + +``` +python3 main.py --consumer-key ${CONSUMER_KEY} --consumer-secret ${CONSUMER_SECRET} +``` + +Open the URL to click on "Authorize app". Go back to the console and write the generated code. + +``` +Please go to https://api.twitter.com/oauth/authorize?oauth_token=xxxxx +Code: 0000000 +Generated tokens: +ACCESS_TOKEN = ***** +ACCESS_TOKEN_SECRET = ***** +``` + +Done. + +## How to contribute + +Please check issues to ensure the feature or bug you are facing is not already known. + +Pull requests are highly appreciated. + +Ensure to lint the code before submitting a pull-request: + +``` +docker run -it -v $(pwd):/mnt/ --rm debian:10 bash +apt-get update && apt-get upgrade -y && apt-get install -y python3-pip git +pip3 install pre-commit +cd /mnt +pre-commit run --all-files +``` diff --git a/main.py b/main.py new file mode 100644 index 0000000..6377aaf --- /dev/null +++ b/main.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +from urllib.parse import urlparse + +import argparse +import tweepy +import logging + + +logger = logging.getLogger(__name__) + + +def main(): + + parser = argparse.ArgumentParser() + parser.add_argument('-d', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, + default=logging.INFO, help='print more output') + parser.add_argument('--consumer-key', help='twitter consumer key', required=True) + parser.add_argument('--consumer-secret', help='twitter consumer secret', required=True) + args = parser.parse_args() + + logging.basicConfig(format='%(message)s', level=args.loglevel) + + auth = tweepy.OAuthHandler(args.consumer_key, args.consumer_secret) + + try: + redirect_url = auth.get_authorization_url() + logger.info(f'Please go to {redirect_url}') + except tweepy.TweepError as err: + logger.error('Cannot get request token') + logger.debug(str(err)) + return + + token = urlparse(redirect_url).query.split('=')[1] + + try: + verifier = input('Code: ') + except KeyboardInterrupt: + return + + auth.request_token = {'oauth_token': token, 'oauth_token_secret': verifier} + + try: + auth.get_access_token(verifier) + except tweepy.TweepError as err: + logger.error('Cannot get access token') + logger.debug(str(err)) + return + + access_token = auth.access_token + access_token_secret = auth.access_token_secret + + logger.info('Generated tokens:') + logger.info(f'ACCESS_TOKEN = {access_token}') + logger.info(f'ACCESS_TOKEN_SECRET = {access_token_secret}') + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b56de2b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,27 @@ +appdirs==1.4.3 +CacheControl==0.12.6 +certifi==2019.11.28 +chardet==3.0.4 +colorama==0.4.3 +contextlib2==0.6.0 +distlib==0.3.0 +distro==1.4.0 +html5lib==1.0.1 +idna==2.8 +ipaddr==2.2.0 +lockfile==0.12.2 +msgpack==0.6.2 +oauthlib==3.1.0 +packaging==20.3 +pep517==0.8.2 +progress==1.5 +pyparsing==2.4.6 +PySocks==1.7.1 +pytoml==0.1.21 +requests==2.22.0 +requests-oauthlib==1.3.0 +retrying==1.3.3 +six==1.14.0 +tweepy==3.10.0 +urllib3==1.25.8 +webencodings==0.5.1