def authenticate(email, servicename, doc, http, client_id, client_secret, force_auth=False): """ Authenticates an provided http object, Prompts for user confirmation if necessary, and stores the credentials Args: email: The email address of the user servicename: The service which requires authentication doc: Documentation for the service (for determining scopes) http: The object being authenticated Returns: The authorized object """ tokens_path = googlecl.get_data_path(TOKENS_FILENAME_FORMAT % (email, servicename), create_missing_dir=True) storage = Storage(tokens_path) credentials = storage.get() if credentials is None or credentials.invalid or force_auth: # Works with google-api-python-client-1.0beta2, but not with # beta4. They're working on a way to allow deleting credentials. # storage.put(None) desiredcred = "" for arg in doc["auth"]["oauth2"]["scopes"]: desiredcred = desiredcred + arg + " " FLOW = OAuth2WebServerFlow(client_id, client_secret, scope=desiredcred, user_agent="discoverycl") credentials = run(FLOW, storage) return credentials.authorize(http)
def run_interactive(parser): """Run an interactive shell for the google commands. Keyword arguments: parser: Object capable of parsing a list of arguments via parse_args. """ history_file = googlecl.get_data_path(googlecl.HISTORY_FILENAME, create_missing_dir=True) try: import readline try: readline.read_history_file(history_file) except EnvironmentError: LOG.debug('Could not read history file.') except ImportError: LOG.debug('Could not import readline module.') while True: try: command_string = raw_input('> ') if command_string.startswith('python '): LOG.info('HINT: No need to include "python" in interactive mode') command_string = command_string.replace('python ', '', 1) if command_string.startswith('google '): LOG.info('HINT: No need to include "google" in interactive mode') command_string = command_string.replace('google ', '', 1) if not command_string: continue elif command_string == '?': print_help() elif command_string == 'quit': break else: try: args_list = expand_as_command_line(command_string) except Error, err: LOG.error(err) continue (options, args) = parse_command_line(parser, args_list) run_once(options, args) except (KeyboardInterrupt, ValueError), err: # It would be nice if we could simply unregister or reset the # signal handler defined in the initial if __name__ block. # Windows will raise a KeyboardInterrupt, GNU/Linux seems to also # potentially raise a ValueError about I/O operation. if isinstance(err, ValueError) and \ str(err).find('I/O operation on closed file') == -1: print "Error: " + str(err) LOG.error(err) raise err print '' print 'Quit via keyboard interrupt' break except EOFError: print '' break
def __init__(self, service_name, client, tokens_path=None): """Initializes instance. Args: service_name: Name of the service. client: Client accessing the service that requires authentication. tokens_path: Path to tokens file. Default None to use result from get_data_path() """ self.service = service_name self.client = client if tokens_path: self.tokens_path = tokens_path else: self.tokens_path = googlecl.get_data_path(TOKENS_FILENAME_FORMAT % client.email, create_missing_dir=True)
def authenticate(email, servicename, doc, http, client_id, client_secret, force_auth=False): """ Authenticates an provided http object, Prompts for user confirmation if necessary, and stores the credentials Args: email: The email address of the user servicename: The service which requires authentication doc: Documentation for the service (for determining scopes) http: The object being authenticated Returns: The authorized object """ tokens_path = googlecl.get_data_path(TOKENS_FILENAME_FORMAT % (email, servicename), create_missing_dir=True) storage = Storage(tokens_path) credentials = storage.get() if credentials is None or credentials.invalid or force_auth: # Works with google-api-python-client-1.0beta2, but not with # beta4. They're working on a way to allow deleting credentials. # storage.put(None) desiredcred = "" for arg in doc['auth']['oauth2']['scopes']: desiredcred = desiredcred + arg + ' ' FLOW = OAuth2WebServerFlow(client_id, client_secret, scope=desiredcred, user_agent='discoverycl') credentials = run(FLOW, storage) return credentials.authorize(http)
# limitations under the License. """ Subclass for the Discovery portion of GoogleCL which manages the documentation In charge of saving/loading the API directory, and retrieves and stores the Discovery documents. """ import httplib2 import logging import simplejson as json import googlecl LOG = logging.getLogger(googlecl.LOGGER_NAME) apis_path = googlecl.get_data_path('apis.dat', create_missing_dir=True) SERVICE_BLACKLIST = ['latitude'] LIST_URL = '%s/discovery/v1/apis?preferred=true&pp=0' SERVICE_URL = '%s/discovery/v1/apis/%s/%s/rest' class DocManager(): def __init__(self, local, base_url): self.base_url = base_url self.load() self.apis = {} self.local = local if self.local: if isinstance(self.local, list): # local comes from the config file for filename in self.local: # Be sure to give the correct path.
""" Subclass for the Discovery portion of GoogleCL which manages the documentation In charge of saving/loading the API directory, and retrieves and stores the Discovery documents. """ import httplib2 import logging import simplejson as json import googlecl LOG = logging.getLogger(googlecl.LOGGER_NAME) apis_path = googlecl.get_data_path('apis.dat', create_missing_dir=True) SERVICE_BLACKLIST = ['latitude'] LIST_URL = '%s/discovery/v1/apis?preferred=true&pp=0' SERVICE_URL = '%s/discovery/v1/apis/%s/%s/rest' class DocManager(): def __init__(self, local, base_url): self.base_url = base_url self.load() self.apis = {} self.local = local if self.local: if isinstance(self.local, list): # local comes from the config file for filename in self.local: # Be sure to give the correct path. self.loadDoc(filename) else: