def oauth2_dance(consumer_key, consumer_secret, token_filename=None):
 twitter = Twitter(auth=OAuth2(consumer_key=consumer_key, consumer_secret=consumer_secret), format='', api_version='')
 token = json.loads(twitter.oauth2.token(grant_type='client_credentials'))['access_token']
 if token_filename:
  write_bearer_token_file(token_filename, token)
 return token

def oauth2_dance ( consumer_key , consumer_secret , token_filename = None ) : twitter = Twitter ( auth = OAuth2 ( consumer_key = consumer_key , consumer_secret = consumer_secret ) , format = '' , api_version = '' ) token = json . loads ( twitter . oauth2 . token ( grant_type = 'client_credentials' ) ) [ 'access_token' ] if token_filename : write_bearer_token_file ( token_filename , token ) return token
perform the oauth2 dance to transform a consumer key and secret into a bearer token .