class ScriptableRequestTokenAuthorization(RequestTokenAuthorizationEngine): """A request token process that doesn't need any user input. The RequestTokenAuthorizationEngine is supposed to be hooked up to a user interface, but that makes it difficult to test. This subclass is designed to be easy to test. """ def __init__(self, consumer_name, username, password, choose_access_level, allow_access_levels=[], max_failed_attempts=2, web_root="http://launchpad.dev:8085/"): # Get a request token. self.credentials = Credentials(consumer_name) self.credentials.get_request_token(web_root=web_root) # Initialize the superclass with the new request token. super(ScriptableRequestTokenAuthorization, self).__init__( web_root, consumer_name, self.credentials._request_token.key, allow_access_levels, max_failed_attempts) self.username = username self.password = password self.choose_access_level = choose_access_level def __call__(self, exchange_for_access_token=True): super(ScriptableRequestTokenAuthorization, self).__call__() # Now verify that it worked by exchanging the authorized # request token for an access token. if (exchange_for_access_token and self.choose_access_level != self.UNAUTHORIZED_ACCESS_LEVEL): self.credentials.exchange_request_token_for_access_token( web_root=self.web_root) return self.credentials.access_token return None def open_page_in_user_browser(self, url): """Print a status message.""" print ("[If this were a real application, the end-user's web " "browser would be opened to %s]" % url) def input_username(self, cached_username, suggested_message): """Collect the Launchpad username from the end-user.""" print suggested_message if cached_username is not None: print "Cached email address: " + cached_username return self.username def input_password(self, suggested_message): """Collect the Launchpad password from the end-user.""" print suggested_message return self.password def input_access_level(self, available_levels, suggested_message, only_one_option): """Collect the desired level of access from the end-user.""" print suggested_message print [level['value'] for level in available_levels]
class RequestTokenApp(object): """An application that creates request tokens.""" def __init__(self, web_root, consumer_name, context): """Initialize.""" self.web_root = lookup_web_root(web_root) self.credentials = Credentials(consumer_name) self.context = context def run(self): """Get a request token and return JSON information about it.""" token = self.credentials.get_request_token( self.context, self.web_root, token_format=Credentials.DICT_TOKEN_FORMAT) return simplejson.dumps(token)
class RequestTokenApp(object): """An application that creates request tokens.""" def __init__(self, web_root, consumer_name, context): """Initialize.""" self.web_root = lookup_web_root(web_root) self.credentials = Credentials(consumer_name) self.context = context def run(self): """Get a request token and return JSON information about it.""" token = self.credentials.get_request_token( self.context, self.web_root, token_format=Credentials.DICT_TOKEN_FORMAT) return simplejson.dumps(token)
def get_token_and_login(cls, consumer_name, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, authorizer_class=AuthorizeRequestTokenWithBrowser, allow_access_levels=[], max_failed_attempts=3, version=DEFAULT_VERSION): """Get credentials from Launchpad and log into the service root. This is a convenience method which will open up the user's preferred web browser and thus should not be used by most applications. Applications should, instead, use Credentials.get_request_token() to obtain the authorization URL and Credentials.exchange_request_token_for_access_token() to obtain the actual OAuth access token. This method will negotiate an OAuth access token with the service provider, but to complete it we will need the user to log into Launchpad and authorize us, so we'll open the authorization page in a web browser and ask the user to come back here and tell us when they finished the authorization process. :param consumer_name: The consumer name, as appropriate for the `Consumer` constructor :type consumer_name: string :param service_root: The URL to the root of the web service. :type service_root: string :return: The web service root :rtype: `Launchpad` """ credentials = Credentials(consumer_name) service_root = uris.lookup_service_root(service_root) web_root_uri = URI(service_root) web_root_uri.path = "" web_root_uri.host = web_root_uri.host.replace("api.", "", 1) web_root = str(web_root_uri.ensureSlash()) authorization_json = credentials.get_request_token( web_root=web_root, token_format=Credentials.DICT_TOKEN_FORMAT) authorizer = authorizer_class( web_root, authorization_json['oauth_token_consumer'], authorization_json['oauth_token'], allow_access_levels, max_failed_attempts) authorizer() credentials.exchange_request_token_for_access_token(web_root) return cls(credentials, service_root, cache, timeout, proxy_info, version)
# Free Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program. If not, see <http://www.gnu.org/licenses/>. import subprocess import sys from launchpadlib.credentials import Credentials web_root = "https://launchpad.net/" creds = Credentials("process-merge-requests") url = creds.get_request_token(web_root=web_root) subprocess.call(['xdg-open', url]) print("Once you have authenticated then press enter") sys.stdin.readline() creds.exchange_request_token_for_access_token(web_root=web_root) f = open("credentials", "wb") try: creds.save(f) finally: f.close()
import json, time from launchpadlib.credentials import Credentials from lazr.restfulclient.errors import HTTPError credentials = Credentials("launchpad-reporting-www") request_token_info = credentials.get_request_token(web_root="production") print request_token_info print '**** *** sleeping (while you validate token in the browser) *** ***' time.sleep(15) print '**** *** continuing *** ***' credentials.exchange_request_token_for_access_token(web_root="production") print json.dumps(credentials)
# This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program. If not, see <http://www.gnu.org/licenses/>. import subprocess import sys from launchpadlib.credentials import Credentials web_root="https://launchpad.net/" creds = Credentials("tarmac") url = creds.get_request_token(web_root=web_root) subprocess.call(['xdg-open', url]) print("Once you have authenticated then press enter") sys.stdin.readline() creds.exchange_request_token_for_access_token(web_root=web_root) f = open("credentials", "wb") try: creds.save(f) finally: f.close()
import json, time from launchpadlib.credentials import Credentials from lazr.restfulclient.errors import HTTPError credentials = Credentials("launchpad-reporting-www") request_token_info = credentials.get_request_token(web_root="production") print request_token_info print "**** *** sleeping (while you validate token in the browser) *** ***" time.sleep(15) print "**** *** continuing *** ***" credentials.exchange_request_token_for_access_token(web_root="production") print json.dumps(credentials)