Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 79 lines (59 sloc) 2.5 KB
#!/usr/bin/env python
#
# Copyright 2012 Dominic Rout
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Provides out of bands authentication with Twitter without relying on Cherrypy
Your browser should open to twitter to begin the process.
Tokens will be automatically saved in the format accepted by this library (YAML).
"""
import webbrowser, sys, yaml, os, shutil
from twitterwrapper import access_tokens
from twitterwrapper._utils import copy_default
CONFIG_FILE = "consumer.yaml"
class AuthenticateTwitterOOB(access_tokens.AuthenticationProcess):
def __init__(self, config):
access_tokens.AuthenticationProcess.__init__(self, config)
def run(self):
url = self.generate_authorize_url()
print "Please browse to %s if you are not directed there automatically." % url
webbrowser.open(url)
print "Please enter the PIN provided by Twitter."
pin = input()
token = self.verify_authorization(pin)
print "Access Token:"
print " - oauth_consumer_token = %s" % self.oauth_consumer_token
print " - oauth_consumer_secret = %s" % self.oauth_consumer_secret
print " - oauth_token = %s" % token['access_token_key']
print " - oauth_token_secret = %s" % token['access_token_secret']
print "The tokens above have been saved to access_tokens.yaml"
self.token_store.add_auth(**token)
self.token_store.save()
if __name__ == "__main__":
try:
with open(CONFIG_FILE) as f:
config = yaml.load(f)
process = AuthenticateTwitterOOB(config["oauth"])
process.run()
except IOError:
print "Creating configuration files:"
#try:
copy_default(CONFIG_FILE)
print "Created %s" % CONFIG_FILE
copy_default(access_tokens.DEFAULT_ACCESS_TOKENS_FILE)
print "Created %s" % access_tokens.DEFAULT_ACCESS_TOKENS_FILE
# except IOError:
# raise IOError("Unable to copy file from package. Giving up.")
print "Please update %s to include your Twitter API consumer token and run again." % CONFIG_FILE
sys.exit()