"""
A function for asking the user for their Twitter API keys.
"""

import requests

from requests_oauthlib import OAuth1
from urllib.parse import parse_qs

def handshake():
    consumer_key = input("Please enter your API key: ")
    consumer_secret = input("Please enter your API secret: ")

    # verify that the keys work to get the bearer token
    url = "https://api.twitter.com/oauth2/token"
    params = {"grant_type": "client_credentials"}
    auth = requests.auth.HTTPBasicAuth(consumer_key, consumer_secret)
    try:
        resp = requests.post(url, params, auth=auth)
        resp.raise_for_status()
        result = resp.json()
        bearer_token = result['access_token']
    except Exception as e:
        return None

    answered = False
    while not answered:
        print("\nHow would you like twarc to obtain your user keys?\n\n1) generate access keys by visiting Twitter\n2) manually enter your access token and secret\n")
        answer = input('Please enter your choice [1 or 2] ')
        if answer == "1":
            answered = True
            generate = True
        elif answer == "2":
            answered = True
            generate = False

    if generate:
        request_token_url = 'https://api.twitter.com/oauth/request_token'
        oauth = OAuth1(consumer_key, client_secret=consumer_secret)
        r = requests.post(url=request_token_url, auth=oauth)

        credentials = parse_qs(r.text)
        if not credentials:
            print("\nError: invalid credentials.")
            print("Please check that you are copying and pasting correctly and try again.\n")
            return

        resource_owner_key = credentials.get('oauth_token')[0]
        resource_owner_secret = credentials.get('oauth_token_secret')[0]

        base_authorization_url = 'https://api.twitter.com/oauth/authorize'
        authorize_url = base_authorization_url + '?oauth_token=' + resource_owner_key
        print('\nPlease log into Twitter and visit this URL in your browser:\n%s' % authorize_url)
        verifier = input('\nAfter you have authorized the application please enter the displayed PIN: ')

        access_token_url = 'https://api.twitter.com/oauth/access_token'
        oauth = OAuth1(consumer_key,
                       client_secret=consumer_secret,
                       resource_owner_key=resource_owner_key,
                       resource_owner_secret=resource_owner_secret,
                       verifier=verifier)
        r = requests.post(url=access_token_url, auth=oauth)
        credentials = parse_qs(r.text)

        if not credentials:
            print('\nError: invalid PIN')
            print('Please check that you entered the PIN correctly and try again.\n')
            return

        access_token = resource_owner_key = credentials.get('oauth_token')[0]
        access_token_secret = credentials.get('oauth_token_secret')[0]

        screen_name = credentials.get('screen_name')[0]
    else:
        access_token = input("Enter your Access Token: ")
        access_token_secret = input("Enter your Access Token Secret: ")
        screen_name = "default"

    return {
        "consumer_key": consumer_key,
        "consumer_secret": consumer_secret,
        "access_token": access_token,
        "access_token_secret": access_token_secret
    }

@@ -167,16 +167,642 @@
 hake():%0A
+%0A    # Default empty keys%0A    consumer_key = %22%22%0A    consumer_secret = %22%22%0A    access_token = %22%22%0A    access_token_secret = %22%22%0A%0A    bearer_token = input(%0A        %22Please enter your Bearer Token (Leave blank to continue on to API key and secret configuration): %22%0A    )%0A%0A    if bearer_token:%0A        continue_adding = input(%0A            %22(Optional) Add API keys and secrets for user mode authentication %5By or n%5D? %22%0A        )%0A%0A        # Save a config with just the bearer_token%0A        if continue_adding.lower() != %22y%22:%0A            return %7B%22bearer_token%22: bearer_token%7D%0A        else:%0A            %22Configure API keys and secrets.%22%0A%0A
     cons
@@ -3924,15 +3924,54 @@
 n_secret
+,%0A        %22bearer_token%22: bearer_token,
 %0A    %7D%0A

Add configuration option to use the bearer token authentication method