def refresh_cache(labels=None): labels = labels if labels is not None else config.SYSTEM_LABELS.keys() flow = flow_from_clientsecrets( config.CLIENT_SECRET_FILE, scope=config.OAUTH_SCOPE) http = httplib2.Http() try: credentials = OAuth2Credentials.from_json( WF.get_password('gmail_credentials')) if credentials is None or credentials.invalid: credentials = run(flow, PseudoStorage(), http=http) WF.save_password('gmail_credentials', credentials.to_json()) WF.logger.debug('Credentials securely updated') http = credentials.authorize(http) gmail_service = build('gmail', 'v1', http=http) for label in labels: WF.cache_data('gmail_%s' % label.lower(), get_list(http, gmail_service, label)) sleep(2) if not WF.cached_data_fresh('gmail_labels', max_age=300): WF.cache_data('gmail_labels', get_labels(gmail_service)) except PasswordNotFound: WF.logger.debug('Credentials not found') credentials = run(flow, PseudoStorage(), http=http) WF.save_password('gmail_credentials', credentials.to_json()) WF.logger.debug('New Credentials securely saved') except httplib2.ServerNotFoundError: WF.logger.debug('ServerNotFoundError')
def refresh_cache(): wf = Workflow() # Start the OAuth flow to retrieve credentials flow = flow_from_clientsecrets( config.CLIENT_SECRET_FILE, scope=config.OAUTH_SCOPE) http = httplib2.Http() try: credentials = OAuth2Credentials.from_json( wf.get_password('gmail_credentials')) if credentials is None or credentials.invalid: credentials = run(flow, PseudoStorage(), http=http) wf.save_password('gmail_credentials', credentials.to_json()) wf.logger.debug('Credentials securely updated') # Authorize the httplib2.Http object with our credentials http = credentials.authorize(http) # Build the Gmail service from discovery gmail_service = build('gmail', 'v1', http=http) wf.cache_data('gmail_list', get_list(wf, http, gmail_service)) except PasswordNotFound: wf.logger.debug('Credentials not found') credentials = run(flow, PseudoStorage(), http=http) wf.save_password('gmail_credentials', credentials.to_json()) wf.logger.debug('New Credentials securely saved')
def getCredentials(secrets_file, tokens_file, scopes): # create an auth flow in case we need to authenticate auth_flow = flow_from_clientsecrets(secrets_file, scope=scopes, message="Visit the APIs Console <https://code.google.com/apis/console>") # search for existing tokens storage = Storage(tokens_file) credentials = storage.get() if credentials is None or credentials.invalid: run(auth_flow, storage) return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'peacemail.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(constants.CLIENT_SECRET_FILE, constants.AUTHORIZATION_SCOPE) flow.user_agent = constants.APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(self): if not os.path.exists(CLIENT_SECRET_FILE): print("the credentials file is missing. follow the tutorial in the readme so you know what to do") exit() """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'drive-python-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def __init__(self,db): # Path to the client_secret.json file downloaded from the Developer Console CLIENT_SECRET_FILE = 'client_secret.json' # Check https://developers.google.com/gmail/api/auth/scopes for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly' # Location of the credentials storage file STORAGE = Storage('gmail.storage') # Start the OAuth flow to retrieve credentials self.flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE) self.http = httplib2.Http() # Try to retrieve credentials from storage or run the flow to generate them self.credentials = STORAGE.get() if self.credentials is None or self.credentials.invalid: self.credentials = run(self.flow, STORAGE, http=self.http) # Authorize the httplib2.Http object with our credentials self.http = self.credentials.authorize(self.http) # Build the Gmail service from discovery self.mailbox = build('gmail', 'v1', http=self.http) self.messages = self.mailbox.users().messages() # Store connection to the database self.db = db
def _Authenticate(self, http, needs_auth): """Pre or Re-auth stuff... This will attempt to avoid making any OAuth related HTTP connections or user interactions unless it's needed. Args: http: An 'Http' object from httplib2. needs_auth: If the user has already tried to contact the server. If they have, it's OK to prompt them. If not, we should not be asking them for auth info--it's possible it'll suceed w/o auth, but if we have some credentials we'll use them anyway. Raises: AuthPermanentFail: The user has requested non-interactive auth but the token is invalid. """ if needs_auth and (not self.credentials or self.credentials.invalid): if self.refresh_token: logger.debug("_Authenticate and skipping auth because user explicitly " "supplied a refresh token.") raise AuthPermanentFail("Refresh token is invalid.") logger.debug("_Authenticate and requesting auth") flow = client.OAuth2WebServerFlow( client_id=self.client_id, client_secret=self.client_secret, scope=self.scope, user_agent=self.user_agent ) self.credentials = tools.run(flow, self.storage) if self.credentials and not self.credentials.invalid: if not self.credentials.access_token_expired or needs_auth: logger.debug("_Authenticate configuring auth; needs_auth=%s", needs_auth) self.credentials.authorize(http) return logger.debug("_Authenticate skipped auth; needs_auth=%s", needs_auth)
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ try: import argparse flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() except ImportError: flags = None home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'calendar-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 credentials = tools.run(flow, store) print 'Storing credentials to ' + credential_path return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('.') credential_dir = os.path.join(home_dir,'') #,'Google Drive/G/IT/Development/ETL/.credentials') #JLD - customized path to working directory if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'client_secret.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ # TODO: fetch the path from the current dir home_dir = '/home/paraboul' credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'calendar-python-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') #/Users/TMD credential_dir = os.path.join(home_dir, '.credentials') #This line adds the directory .credentials to my home_dir so /Users/TMD/.credentials print credential_dir #If the credentials directory does not exist, the conditional creates the credential #directory and joins it's json file containing the id and pwd to the credentials directory if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'task-manager.json') store = oauth2client.file.Storage(credential_path) #Creates an object named store <oauth2client.file.Storage object at 0x10324c6d0> credentials = store.get() #Applies the get() method on the object and stores the new object as credentials if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print 'Storing credentials to ' + credential_path return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.eam') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'access_token.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() flags = tools.argparser.parse_args(args=[]) if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=' '.join(SCOPES)) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser("~") credential_dir = os.path.join(home_dir, ".credentials") if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, "calendar-quickstart.json") store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 credentials = tools.run(flow, store) print "Storing credentials to " + credential_path return credentials
def auth_credentials(self): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ credential_dir = './' if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, self._session_file) store = oauth2client.file.Storage(credential_path) self._credentials = store.get() if not self._credentials or self._credentials.invalid: flow = client.flow_from_clientsecrets(self._client_secret_file, self._scopes) flow.user_agent = self._application_name flow.params['access_type'] = 'offline' if flags: self._credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 self._credentials = tools.run(flow, store) # Authorize with HTTP self._http = self._credentials.authorize(httplib2.Http()) # Setup Service in order to upload file self._service = discovery.build('drive', 'v2', http=self._http)
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('C:\\Users\\nmishra\\PycharmProjects\\EDI\\') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'gmail-python-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. prease CLIENT_SECRET_FILE is puts same directory with botrun.py """ if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, CLIENT_SECRET_FILE) store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.gflows') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'credential.json') store = Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: script_path = os.path.dirname(os.path.realpath(__file__)) flow = client.flow_from_clientsecrets(os.path.join(script_path,CLIENT_SECRET_FILE), SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def __init__(self, api_url='https://www.googleapis.com/sql/v1/', oauth_credentials_path=None, oauth_storage=None, developer_key=None): """Constructs an RdbmsGoogleApiClient. Args: api_url: The base of the URL for the rdbms Google API. oauth_credentials_path: The filesystem path to use for OAuth 2.0 credentials storage. oauth_storage: A client.Storage instance to use for OAuth 2.0 credential storage instead of the default file based storage. developer_key: A Google APIs developer key to use when connecting to the SQL service. """ self._api_url = api_url self._developer_key = developer_key if oauth_storage is None: if oauth_credentials_path is None: oauth_credentials_path = os.path.expanduser( rdbms.OAUTH_CREDENTIALS_PATH) oauth_storage = oauth_file.Storage(oauth_credentials_path) credentials = oauth_storage.get() if credentials is None or credentials.invalid: from oauth2client import tools credentials = tools.run(GetFlow(), oauth_storage) self._transport = credentials.authorize(httplib2.Http())
def __init__(self): # If modifying these scopes, delete your previously saved credentials # at ~/.credentials/drive_credentials.json # https://developers.google.com/drive/v3/web/about-auth#OAuth2Authorizing SCOPES = 'https://www.googleapis.com/auth/drive' CLIENT_SECRET_FILE = 'client_secret_oauth.json' APPLICATION_NAME = 'BenchBox upload file to drive' CACHED_CREDENTIAL_FILE = 'drive_credentials.json' # inserting file to certain folder id ... lookup required, none transactional operation error prone..*[]: self.whoami = (self).__class__.__name__ print self.whoami curr_dir = os.path.expanduser('.') credential_dir = os.path.join(curr_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, CACHED_CREDENTIAL_FILE) self.store = oauth2client.file.Storage(credential_path) self.credentials = self.store.get() if not self.credentials or self.credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME self.credentials = tools.run(flow, self.store) print 'New credentials Storing to ' + credential_path else: print "Credentials already cached!" http = self.credentials.authorize(httplib2.Http()) self.service = discovery.build('drive', 'v3', http=http) self.root_folder = self.service.files().get(fileId='root').execute() print self.root_folder
def get_credentials(userID): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) user_dir = os.path.join(project_dir,'Data/Users') user_dir2 = os.path.join(user_dir,'{}'.format(userID)) credential_dir = os.path.join(user_dir2, '.credentials') #os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'user{}-cred.json'.format(userID)) store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def _youtube(cls, authenticate=False): while True: try: if not authenticate: return build(cls.YOUTUBE_API_SERVICE_NAME, cls.YOUTUBE_API_VERSION, developerKey=cls.DEVELOPER_KEY) storage = Storage('mpris-youtube', os.getlogin()) credentials = storage.get() if credentials is None: flow = OAuth2WebServerFlow( client_id=cls.CLIENT_ID, client_secret=cls.CLIENT_SECRET, scope=cls.AUTH_SCOPE #redirect_uri='urn:ietf:wg:oauth:2.0:oob' ) credentials = run(flow, storage) http = httplib2.Http() credentials.authorize(http) return build( cls.YOUTUBE_API_SERVICE_NAME, cls.YOUTUBE_API_VERSION, http=http) except Exception as e: print e time.sleep(3)
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ #home_dir = os.path.expanduser('~') #credential_dir = os.path.join(home_dir, '.credentials') #if not os.path.exists(credential_dir): # os.makedirs(credential_dir) #credential_path = os.path.join(credential_dir, #'drive-python-quickstart.json') store = Storage('/home/hadn/hang.cucku/drive-python-quickstart.json') credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) #print('Storing credentials to ' + credential_path) return credentials
def gdrive_login(self): """ Logins to the gdrive using the your credentials - no input needed""" # Authenticate and get a service object attempt_1=0 while attempt_1 < 4: try: # Send in client secret and client ID to the authetication server. Need to set this up in google developer console to get the client secrets and ID # Then need to also activate google analytics in the allowed applications flow = OAuth2WebServerFlow( self.client_id, self.client_secret, 'https://www.googleapis.com/auth/drive') # Stores the credentials in credentials.dat (i think) storage = Storage('credentials_gdrive.dat') credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(flow, storage) # Use the credentials to get authentication? # Finally if this is the first time, your browser should pop to ask for login and permission allowing app http = httplib2.Http() http = credentials.authorize(http) self.service_gd = build('drive', 'v2', http=http) attempt_1=100 except Exception as e_connection: attempt_1+=1 self.logger.info('Exception is: '+str(e_connection)) self.logger.info('Attempt number '+str(attempt_1)) time.sleep(7) pass print ('Exception is: '+str(e_connection)+'\n'+'Attempt number '+str(attempt_1))
def __build_service(self): """ Creates the service object and based on the credentials stored in config.py """ flow = OAuth2WebServerFlow( client_id=config.client_id, client_secret=config.client_secret, scope='https://www.googleapis.com/auth/calendar', user_agent=config.user_agent) storage = Storage(self.credentials_filename) credentials = storage.get() if credentials is None or credentials.invalid == True: credentials = run(flow, storage) http = httplib2.Http() http = credentials.authorize(http) service = build( serviceName="calendar", version="v3", http=http, developerKey=config.developer_key) return service
def get_credentials(): print "START - get_credentials" #directory home home_dir = os.path.expanduser('~') #directory delle credenziali credential_dir = os.path.join(home_dir, '.credentials') #se non esiste la crea if not os.path.exists(credential_dir): os.makedirs(credential_dir) #path al file delle credenziali credential_path = os.path.join(credential_dir, 'gmail-quickstart.json') #prende le credenziali dal file store = oauth2client.file.Storage(credential_path) credentials = store.get() #se non ci sono o sono invalide richiede l'auth if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 credentials = tools.run(flow, store) print 'Storing credentials to ' + credential_path else: print 'Credenziali gia presenti' print "FINISH - get_credentials" return credentials
def Authenticate(self): f = Auth(KEYS_FILE) # OAuth 2.0 Authentication flow = OAuth2WebServerFlow( client_id=f.GetClientId(), client_secret=f.GetClientSecret(), scope='https://www.googleapis.com/auth/tasks', user_agent='Tasky/v1') # If credentials don't exist or are invalid, run through the native client # flow. The Storage object will ensure that if successful, the good # Credentials will get written back to a file. storage = Storage(os.path.join(TASKY_DIR, 'tasks.dat')) credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(flow, storage) http = httplib2.Http() http = credentials.authorize(http) # The main Tasks API object. self.service = build( serviceName='tasks', version='v1', http=http, developerKey=f.GetApiKey())
def get_mailbox(self): """Gets valid user credentials from file""" home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'gmail-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 credentials = tools.run(flow, store) print 'Storing credentials to ' + credential_path http = credentials.authorize(httplib2.Http()) service = discovery.build('gmail', 'v1', http=http) return service.users()
def get_credentials(): home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'gmail-python-test.json') #this file is where the account's credentials go after the authentication with the secret store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: ''' If modifying these scopes, delete your previously saved credentials at ~/.credentials/gmail-python-test.json''' scopes = 'https://mail.google.com/' # more info about scopes in https://developers.google.com/gmail/api/auth/scopes#gmail_scopes clientSecretFile = 'client_secret.json' """ this is important, your own json file with the OAUTH2 Google access to the Gmail Api more info in https://developers.google.com/identity/protocols/OAuth2 and https://developers.google.com/api-client-library/python/guide/aaa_oauth I put an example of client_secret.json file you can download your own file in the developer console, more info in https://support.google.com/cloud/answer/6158857?hl=en&ref_topic=6262490 in my case, I generated the file and didn't changed anything of it """ appName = 'Gmail API Python Test' flow = client.flow_from_clientsecrets(clientSecretFile, scopes) flow.user_agent = appName if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(flags): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ if flags.client_secret: client_secret = flags.client_secret else: client_secret = DEFAULT_CLIENT_SECRET_FILE script_dir = os.path.dirname(os.path.realpath(__file__)) credential_dir = os.path.join(script_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'drive-python-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(client_secret, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def __init__(self, job_description, saga_url, pilot_compute_description): self.job_description = job_description self.saga_url = saga_url self.pilot_compute_description = pilot_compute_description self.image_url = GCE_IMAGE_URL if self.pilot_compute_description.has_key("vm_id"): self.image_url = self.pilot_compute_description["vm_id"] self.machine_type = "https://www.googleapis.com/compute/v1beta12/projects/bigjob-pilot/machine-types/n1-standard-1" if self.pilot_compute_description.has_key("vm_type"): self.machine_type = self.pilot_compute_description["vm_type"] self.location = "https://www.googleapis.com/compute/v1beta12/projects/bigjob-pilot/zones/us-east1-a" if self.pilot_compute_description.has_key("vm_location"): self.location = self.pilot_compute_description["vm_location"] self.id="bigjob-" + str(uuid.uuid1()) self.network_ip=None # Do OAUTH authentication storage = Storage('gce.dat') self.credentials = storage.get() if self.credentials is None or self.credentials.invalid == True: flow = OAuth2WebServerFlow( client_id=OAUTH2_CLIENT_ID, client_secret=OAUTH2_CLIENT_SECRET, scope='https://www.googleapis.com/auth/compute', user_agent='bigjob-client/1.0') self.credentials = run(flow, storage)
def twplaylist(): flow = OAuth2WebServerFlow(yt_id, yt_secret, scope) storage = Storage('credentials.dat') parser = argparse.ArgumentParser(parents=[tools.argparser]) flags = parser.parse_args() credentials = tools.run_flow(flow, storage, flags) if credentials is None or credentials.invalid: credentials = run(flow, storage) global http http = httplib2.Http() http = credentials.authorize(http) global youtube youtube = build('youtube', 'v3', http=http) global twitter twitter = tweepy.API(tweepy.OAuthHandler(t_id, t_secret)) user = twitter.get_user(t_user) last_id = '' while True: try: ytlinks = [] if last_id: print 'Grabbing ' + str( t_count ) + ' latest tweets from ' + user.screen_name + ' since ' + str( last_id) statuses = twitter.user_timeline(id=t_user, since_id=int(last_id), count=t_count) else: print 'Grabbing ' + str( t_count) + ' latest tweets from ' + user.screen_name statuses = twitter.user_timeline(id=t_user, count=t_count) if len(statuses) > 0: last_id = statuses[0].id for status in statuses: if (match in status.text.lower()) and ( status.entities.has_key('urls')): url = status.entities.get('urls')[0].get( 'expanded_url') ytcomlink = re.findall(ytcomReg, url) ytbelink = re.findall(ytbeReg, url) if ytbelink != []: ytlinks += ytbelink if ytcomlink != []: ytlinks += ytcomlink print 'Adding ' + str(len(ytlinks)) + ' video(s)' for video in ytlinks: insert = youtube.playlistItems().insert( part="snippet", body=dict( snippet=dict(playlistId=playlist, resourceId=dict(kind="youtube#video", videoId=video)))) response = insert.execute() else: print 'No new tweets since last attempt' print 'Waiting ' + str(sleep) + ' seconds' time.sleep(int(sleep)) except Exception, e: print e
def prepare_credentials(): storage = Storage(TOKEN_FILE_NAME) credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(FLOW, storage) return credentials
# try: # import argparse # flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() # except ImportError: # flags = None SCOPES = 'https://www.googleapis.com/auth/drive.file' + ' ' + 'https://www.googleapis.com/auth/drive.readonly' CLIENT_SECRET = 'client_secret.json' # flags = tools.argparser.parse_args(args=[]) store = file.Storage('storage.json') credz = store.get() if not credz or credz.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET, SCOPES) credz = tools.run_flow(flow, store, flags) \ if flags else tools.run(flow, store) DRIVE = build('drive', 'v3', http=credz.authorize(Http())) #SERVICE = build(API, VERSION, developerKey = API_KEY) # files= SERVICE.files().list().execute().get('items',[]) # for f in files: # print (f['title'], f['mimeType']) # FILES = ( # ('Win10_1607_English_x64.iso', False) # )https://drive.google.com/open?id=0BxtzJzlgRwjrVFJ3bFVTNlEtaUU # res, data = DRIVE._http.request(res[]) #file_id = '0B9ejphBUrJL_U1gyanNzNTJrQUE'
CLIENT_SECRET_FILE = '/usr/share/adafruit/webide/repositories/my-pi-projects/Doorbell/client_secret_935106472194-ntvvnbqtgcpnd2qu7akrk6g8tu225br4.apps.googleusercontent.com.json' # Check https://developers.google.com/gmail/api/auth/scopes for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.compose' # Location of the credentials storage file STORAGE = Storage('gmail.storage') # Start the OAuth flow to retrieve credentials flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE) http = httplib2.Http() # Try to retrieve credentials from storage or run the flow to generate them credentials = STORAGE.get() if credentials is None or credentials.invalid: credentials = run(flow, STORAGE, http=http) # Authorize the httplib2.Http object with our credentials http = credentials.authorize(http) # Build the Gmail service from discovery gmail_service = build('gmail', 'v1', http=http) # create a message to send message = MIMEText("Hi, \n\nSomeone knocked on your door at" + strftime("%l:%M %p on %d-%m-%Y") + ".\n\nHave a great day!") message['to'] = EMAIL message['from'] = "*****@*****.**" message['subject'] = "Ding Dong at " + strftime("%l:%M %p on %d-%m-%Y") body = {'raw': base64.b64encode(message.as_string())}
import json try: import argparse flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() except ImportError: flags = None SCOPES = "https://www.googleapis.com/auth/calendar" store = file.Storage('G_files/storage.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('G_files/client_secrets.json', SCOPES) creds = tools.run_flow(flow, store, flags) if flags else tools.run( flow, store) CAL = build('calendar', 'v3', http=creds.authorize(Http())) GMT_OFF = '+02:00' CALENDAR_ID = '*****@*****.**' def coloring(): with open('Data/log.json', 'r') as f: data = json.load(f) for eventId in data: event = CAL.events().get(calendarId=CALENDAR_ID, eventId=eventId).execute()
import httplib2 from oauth2client.file import Storage from oauth2client.client import AccessTokenRefreshError from oauth2client.client import OAuth2WebServerFlow from oauth2client.tools import run FLOW = OAuth2WebServerFlow( # Client id, secret from http://code.google.com/apis/console#access client_id='146153464872.apps.googleusercontent.com', client_secret='iiZSwCtCMqCIPYWltpprxY41', scope='https://www.googleapis.com/auth/taskqueue', user_agent='python-gae-queues-demo/1.0') # If the Credentials don't exist or are invalid run through the native client # flow. The Storage object will ensure that if successful the good Credentials # will get written back to a file. storage = Storage('credentials.dat') credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(FLOW, storage) # Create an httplib2.Http object to handle our HTTP requests and authorize it # with our good Credentials. http = httplib2.Http() http = credentials.authorize(http)
#Download exceptions file CLIENT_SECRET = 'tl_client_secret.json' SCOPES = [ 'https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.appdata', 'https://www.googleapis.com/auth/drive.apps.readonly', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.readonly' ] store = file.Storage('storage.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET, ' '.join(SCOPES)) creds = tools.run(flow, store) DRIVE = build('drive', 'v2', http=creds.authorize(Http())) file_Id = str(data['DRIVE_FILE']) gFile = DRIVE.files().get(fileId = file_Id).execute() logger.info('Downloading iTunesU Exception list...') download_file(DRIVE, gFile) #Get the site exceptions that we are checking with open('itunes_exceptions.csv') as f: for line in f: exceptions.append(line.rstrip('\n\r ')) for n, exception in enumerate(exceptions): splits = exception.split(',') siteId = splits[0] exceptions[n] = siteId for exception in exceptions:
home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'youtube-python-quickstart.json') store = Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRETS_FILE, SCOPES) flow.user_agent = '206Youtube' if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) youtube = build('youtube', 'v3', credentials = credentials) os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' fileNames = ['emails.json', 'newsMessages.json', 'politicalAnalysis.json', 'youtubeData.json'] timeURL = 'https://plot.ly/~M4LL0C/4/popular-youtube-videos/' politicalURL = 'https://plot.ly/~M4LL0C/6/average-political-tendencies/' print('A political and temporal analysis of the Internet') print('-------------(At the most basic level)-----------') newData = input('Wanna delete the cache and get some fresh data? (Y/N): ').lower() if newData[0] == 'y':
def get_outh_credentials(client_secret_file, credential_dir=None, outh_nonlocal=False): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. :param client_secret_file: path to outh2 client secret file :param credential_dir: path to directory where tokens should be stored 'global' if you want to store in system-wide location None if you want to store in current script directory :param outh_nonlocal: if the authorization should be done in another computer, this will provide a url which when run will ask for credentials :return Credentials, the obtained credential. """ lflags = flags if credential_dir == 'global': home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) elif not credential_dir: credential_dir = os.getcwd() else: pass # verify credentials directory if not os.path.isdir(credential_dir): raise IOError(2, "Credential directory does not exist.", credential_dir) credential_path = os.path.join(credential_dir, 'sheets.googleapis.com-python.json') # check if refresh token file is passed with warnings.catch_warnings(): warnings.simplefilter("ignore") try: store = Storage(client_secret_file) credentials = store.get() except KeyError: credentials = None # else try to get credentials from storage if not credentials or credentials.invalid: try: with warnings.catch_warnings(): warnings.simplefilter("ignore") store = Storage(credential_path) credentials = store.get() except KeyError: credentials = None # else get the credentials from flow if not credentials or credentials.invalid: # verify client secret file if not os.path.isfile(client_secret_file): raise IOError(2, "Client secret file does not exist.", client_secret_file) # execute flow flow = client.flow_from_clientsecrets(client_secret_file, SCOPES) flow.user_agent = 'pygsheets' if lflags: lflags.noauth_local_webserver = outh_nonlocal credentials = tools.run_flow(flow, store, lflags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def main(): # Create a Storage object. This object holds the credentials that your # application needs to authorize access to the user's data. The name of the # credentials file is provided. If the file does not exist, it is # created. This object can only hold credentials for a single user, so # as-written, this script can only handle a single user. storage = Storage('credentials.dat') # The get() function returns the credentials for the Storage object. If no # credentials were found, None is returned. credentials = storage.get() # If no credentials are found or the credentials are invalid due to # expiration, new credentials need to be obtained from the authorization # server. The oauth2client.tools.run() function attempts to open an # authorization server page in your default web browser. The server # asks the user to grant your application access to the user's data. # If the user grants access, the run() function returns new credentials. # The new credentials are also stored in the supplied Storage object, # which updates the credentials.dat file. if credentials is None or credentials.invalid: credentials = run(flow, storage) # Create an httplib2.Http object to handle our HTTP requests, and authorize it # using the credentials.authorize() function. http = httplib2.Http() http = credentials.authorize(http) # The apiclient.discovery.build() function returns an instance of an API service # object can be used to make API calls. The object is constructed with # methods specific to the calendar API. The arguments provided are: # name of the API ('calendar') # version of the API you are using ('v3') # authorized httplib2.Http() object that can be used for API calls service = build('calendar', 'v3', http=http) try: today = datetime.datetime.today() today_formatted = today.strftime("%Y-%m-%dT00:00:00Z") today_plus_seven_days = today + datetime.timedelta(days=7) today_plus_seven_days_formatted = today_plus_seven_days.strftime( "%Y-%m-%dT23:59:59Z") # The Calendar API's events().list method returns paginated results, so we # have to execute the request in a paging loop. First, build the # request object. The arguments provided are: # primary calendar for user request = service.events().list( calendarId='primary', timeMin=today_formatted, timeMax=today_plus_seven_days_formatted, orderBy="startTime", singleEvents=True) # Loop until all pages have been processed. while request != None: # Get the next page. response = request.execute() # Accessing the response like a dict object with an 'items' key # returns a list of item objects (events). prev_human_readable_date = '' for event in response.get('items', []): event_start = event.get('start') event_start_datetime = event_start[ 'dateTime'] if 'dateTime' in event_start else None event_start_date = event_start[ 'date'] if 'date' in event_start else event_start_datetime[: 10] event_end = event.get('end') event_end_datetime = event_end[ 'dateTime'] if 'dateTime' in event_end else None t = time.strptime(event_start_date, '%Y-%m-%d') newdate = datetime.date(t.tm_year, t.tm_mon, t.tm_mday) day_of_the_week = newdate.strftime('%A') day = newdate.strftime('%d').lstrip('0') month_full = newdate.strftime('%B') year_full = newdate.strftime('%Y') human_readable_date = month_full + " " + day + ", " + year_full human_readable_start_time = event_start_datetime[ 11:16] + " - " if event_start_datetime else 'N/A' human_readable_end_time = event_end_datetime[ 11:16] if event_end_datetime else '' if human_readable_date != prev_human_readable_date: print '\n' + repr(day_of_the_week) + ", " + repr( human_readable_date) period = repr(human_readable_start_time + human_readable_end_time) organizer = event.get('organizer') # The event object is a dict object with a 'summary' key. print repr( event.get('summary', 'NO SUMMARY') + ', (' + organizer['displayName'] + '), ' + period) prev_human_readable_date = human_readable_date # Get the next request object by passing the previous request object to # the list_next method. request = service.events().list_next(request, response) except AccessTokenRefreshError: # The AccessTokenRefreshError exception is raised if the credentials # have been revoked by the user or they have expired. print( 'The credentials have been revoked or expired, please re-run' 'the application to re-authorize')
def youtube_view(request): yt_service = gdata.youtube.service.YouTubeService() yt_service.ssl = True YOUTUBE_SCOPES = [ "https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/yt-analytics.readonly" ] YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" YOUTUBE_ANALYTICS_API_SERVICE_NAME = "youtubeAnalytics" YOUTUBE_ANALYTICS_API_VERSION = "v1" CLIENT_SECRETS_FILE = 'client_secrets.json' now = datetime.now() one_day_ago = (now - timedelta(days=1)).strftime("%Y-%m-%d") one_week_ago = (now - timedelta(days=7)).strftime("%Y-%m-%d") parser = OptionParser() parser.add_option( "--metrics", dest="metrics", help="Report metrics", default= "views,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares") parser.add_option("--dimensions", dest="dimensions", help="Report dimensions", default="video") parser.add_option("--start-date", dest="start_date", help="Start date, in YYYY-MM-DD format", default=one_week_ago) parser.add_option("--end-date", dest="end_date", help="End date, in YYYY-MM-DD format", default=one_day_ago) parser.add_option("--start-index", dest="start_index", help="Start index", default=1, type="int") parser.add_option("--max-results", dest="max_results", help="Max results", default=10, type="int") parser.add_option("--sort", dest="sort", help="Sort order", default="-views") (options, args) = parser.parse_args() flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=" ".join(YOUTUBE_SCOPES)) storage = Storage("%s-oauth2.json" % sys.argv[0]) credentials = storage.get() credentials = run(flow, storage) http = credentials.authorize(httplib2.Http()) youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=http) youtube_analytics = build(YOUTUBE_ANALYTICS_API_SERVICE_NAME, YOUTUBE_ANALYTICS_API_VERSION, http=http) channels_response = youtube.channels().list(mine=True, part="id").execute() greetings = [] v = [] for channel in channels_response.get("items", []): channel_id = channel["id"] analytics_response = youtube_analytics.reports().query( ids="channel==%s" % channel_id, metrics=options.metrics, dimensions=options.dimensions, start_date=options.start_date, end_date=options.end_date, start_index=options.start_index, max_results=options.max_results, sort=options.sort).execute() print "Analytics Data for Channel %s" % channel_id greetings.append("Analytics Data for Channel %s" % channel_id) for column_header in analytics_response.get("columnHeaders", []): print "%-20s" % column_header["name"], for row in analytics_response.get("rows", []): for value in row: print "%-20s" % value v.append("%-20s" % value) print v print dir(youtube_analytics) data = { 'greetings': greetings, 'analytics_response': channels_response.get("items", []), 'v': v } return render(request, 'yt_console.html', data)
from __future__ import print_function
def get_credentials(scopes, oauth_client_application_name, oauth_client_secret_json_file): """ ------------------------------------------------------------------------ Title: ------------------------------------------------------------------------ Gets valid user credentials from storage. Called by function: oauth_test_case_generator_get_spreadsheet_data::oauth_test_case_generator_get_spreadsheet_data() ------------------------------------------------------------------------ Description: ------------------------------------------------------------------------ This function authenticates OAuth client with Google to get the access to the private Google Spreadsheets. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. ------------------------------------------------------------------------ Resources: ------------------------------------------------------------------------ Code brought from: https://developers.google.com/sheets/api/quickstart/python ------------------------------------------------------------------------ Parameters: ------------------------------------------------------------------------ :param scopes: Modifying scopes. :type scopes: string Example: scopes = 'https://www.googleapis.com/auth/spreadsheets.readonly' :param oauth_client_application_name: OAuth client application name. :type oauth_client_application_name: string :param oauth_client_secret_json_file: OAuth client secret JSON file path. :type oauth_client_secret_json_file: string ------------------------------------------------------------------------ Return: ------------------------------------------------------------------------ :return credentials: Credentials, the obtained credential. :rtype credentials: Credentials """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join( credential_dir, 'sheets.googleapis.com-python-client_secret.json') # If modifying these scopes, delete your previously saved credentials # at ~/.credentials/sheets.googleapis.com-python-quickstart.json # scopes = 'https://www.googleapis.com/auth/spreadsheets.readonly' store = Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(oauth_client_secret_json_file, scopes) flow.user_agent = oauth_client_application_name if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) # Return the obtained credential. return credentials
def execute(wf): if len(wf.args): if 'reopen' in wf.args[0]: open_alfred() return 0 query = json.loads(wf.args[0]) # Start the OAuth flow to retrieve credentials flow = flow_from_clientsecrets(config.CLIENT_SECRET_FILE, scope=config.OAUTH_SCOPE) http = httplib2.Http() try: credentials = OAuth2Credentials.from_json( wf.get_password('gmail_credentials')) if credentials is None or credentials.invalid: credentials = run(flow, PseudoStorage(), http=http) wf.save_password('gmail_credentials', credentials.to_json()) # Authorize the httplib2.Http object with our credentials http = credentials.authorize(http) # Build the Gmail service from discovery service = build('gmail', 'v1', http=http) except PasswordNotFound: wf.logger.error('Credentials not found') return 0 try: thread_id = query['thread_id'] except KeyError: return 0 message_id = query.get('message_id') target = None if 'action' in query: if query['action'] == 'deauthorize': wf.delete_password('gmail_credentials') wf.clear_cache() print "Workflow deauthorized." return 0 elif query['action'] == 'mark_as_read': mark_conversation_as_read(service, thread_id) target = query.get('query') elif query['action'] == 'mark_as_unread': mark_conversation_as_unread(service, thread_id) target = query.get('query') elif query['action'] == 'archive_conversation': refresh_cache(archive_conversation(service, thread_id)) elif query['action'] == 'trash_message': refresh_cache(trash_message(service, message_id)) target = query.get('label') elif query['action'] == 'move_to_inbox': refresh_cache(move_to_inbox(service, message_id)) target = query.get('label') elif query['action'] == 'trash_conversation': refresh_cache(trash_conversation(service, thread_id)) target = query.get('label') elif query['action'] == 'reply': if 'message' in query: send_reply(wf, service, thread_id, query['message']) else: print 'No message found.' target = query.get('query') elif query['action'] == 'label': if 'label' in query: add_label(service, thread_id, query['label']) else: print 'No label found.' target = query.get('query') elif query['action'] == 'open': open_message(wf, thread_id) if 'label_id' in query: refresh_cache([query['label_id']]) return 0 else: wf.logger.debug('No action defined') return 0 open_alfred(target)
from httplib2 import Http import json import os, pwd uid = pwd.getpwuid(os.getuid())[0] storage = Storage("Jeff's Sample Python App", uid) http = Http( disable_ssl_certificate_validation=True) # Should not have to do this! flow = flow_from_clientsecrets('client_secrets.json', scope='public_api', redirect_uri='http://localhost:8080/') credentials = storage.get() if credentials == None: credentials = run(flow, storage, http=http) storage.put(credentials) print "access_token:" + credentials.access_token http = credentials.authorize(http) # Snag the user's home network resp, content = http.request('https://api.alfresco.com') networkList = json.loads(content) homeNetwork = networkList['list']['entries'][0]['entry']['id'] print "Your home network appears to be: %s" % homeNetwork # Find out what sites the user can see in his home network resp, content = http.request( 'https://api.alfresco.com/%s/public/alfresco/versions/1/sites?maxItems=10'
def set_work_cal(n): print("Extracting Schedule...") sch = week_schedules(int(n)) print("Extraction Complete!") #Example Schedule #sch = {'2016-06-07': ('09:45', '15:30'), '2016-06-10': ('12:00', '18:00'), '2016-06-05': ('10:00', '14:00'), # '2016-06-03': ('17:00', '22:00'), '2016-06-11': ('11:00', '19:00'), # '2016-06-06': ('18:00', '21:45'), '2016-06-08': ('11:00', '17:00')} t = datetime.datetime.now() SCOPES = 'https://www.googleapis.com/auth/calendar' store = file.Storage('storage.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) creds = tools.run_flow(flow, store, flags) \ if flags else tools.run(flow, store) CAL = build('calendar', 'v3', http=creds.authorize(Http())) GMT_OFF = '-05:00' # PDT/MST/GMT-7 workCal = '*****@*****.**' #Deletes all previous work events to make room for new events (to prevent overlapping) events = CAL.events().list( calendarId=workCal, timeMin=rf(string_date(t), '00:00', GMT_OFF), timeMax=rf(string_date(t + datetime.timedelta(weeks=4)), '00:00', GMT_OFF), singleEvents=True).execute() print("Deleting previous schedules added by program...") for event in events['items']: if 'description' in event.keys( ) and 'jcp_weekly' in event['description']: CAL.events().delete(calendarId=workCal, eventId=event['id']).execute() else: pass print("Adding newly extracted schedule...") for key in sch: start = rf(str(key), sch[key][0], GMT_OFF) end = rf(str(key), sch[key][1], GMT_OFF) EVENT = { 'summary': 'Work', 'description': 'Work at JCPenney\'s (automatically added with jcp_weekly)', 'start': { 'dateTime': start, 'timeZone': 'America/Chicago' }, 'end': { 'dateTime': end, 'timeZone': 'America/Chicago' }, 'background': '#aaaaaa', 'foreground': "#abcde1", 'reminders': { 'useDefault': False, 'overrides': [{ 'method': 'popup', 'minutes': 60 }] } } CAL.events().insert(calendarId=workCal, sendNotifications=True, body=EVENT).execute()
def main(argv): """Demos the setting of the access properties by the Groups Settings API.""" usage = 'usage: %prog [options]' parser = OptionParser(usage=usage) parser.add_option('--groupId', help='Group email address') parser.add_option('--whoCanInvite', help='Possible values: ALL_MANAGERS_CAN_INVITE, ' 'ALL_MEMBERS_CAN_INVITE') parser.add_option('--whoCanJoin', help='Possible values: ALL_IN_DOMAIN_CAN_JOIN, ' 'ANYONE_CAN_JOIN, CAN_REQUEST_TO_JOIN, ' 'CAN_REQUEST_TO_JOIN') parser.add_option('--whoCanPostMessage', help='Possible values: ALL_IN_DOMAIN_CAN_POST, ' 'ALL_MANAGERS_CAN_POST, ALL_MEMBERS_CAN_POST, ' 'ANYONE_CAN_POST, NONE_CAN_POST') parser.add_option('--whoCanViewGroup', help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, ' 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, ' 'ANYONE_CAN_VIEW') parser.add_option('--whoCanViewMembership', help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, ' 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, ' 'ANYONE_CAN_VIEW') (options, args) = parser.parse_args() if options.groupId is None: print 'Give the groupId for the group' parser.print_help() return settings = {} if (options.whoCanInvite or options.whoCanJoin or options.whoCanPostMessage or options.whoCanPostMessage or options.whoCanViewMembership) is None: print 'No access parameters given in input to update access permissions' parser.print_help() else: settings = { 'whoCanInvite': options.whoCanInvite, 'whoCanJoin': options.whoCanJoin, 'whoCanPostMessage': options.whoCanPostMessage, 'whoCanViewGroup': options.whoCanViewGroup, 'whoCanViewMembership': options.whoCanViewMembership } # Set up a Flow object to be used if we need to authenticate. FLOW = flow_from_clientsecrets( CLIENT_SECRETS, scope='https://www.googleapis.com/auth/apps.groups.settings', message=MISSING_CLIENT_SECRETS_MESSAGE) storage = Storage('groupsettings.dat') credentials = storage.get() if credentials is None or credentials.invalid: print 'invalid credentials' # Save the credentials in storage to be used in subsequent runs. credentials = run(FLOW, storage) # Create an httplib2.Http object to handle our HTTP requests and authorize it # with our good Credentials. http = httplib2.Http() http = credentials.authorize(http) service = build('groupssettings', 'v1', http=http) access_settings(service=service, groupId=options.groupId, settings=settings)
def generateAuth(self): self.credentials = run(self.FLOW, self.storage)
def oauth2login(user): """ Attempts to authenticate user for Galah using Google OAuth2. """ # Google OAuth2 from oauth2client.tools import run from oauth2client.file import Storage from oauth2client.client import OAuth2WebServerFlow import httplib2, json # Get client_id and client_secret from server try: google_api_keys = json.loads(call_backend("get_oauth2_keys")) except requests.exceptions.ConnectionError as e: print >> sys.stderr, "Could not connect with the given url '%s':" \ % config["galah_host"] print >> sys.stderr, "\t" + str(e) exit(1) # Google OAuth2 flow object to get user's email. flow = OAuth2WebServerFlow( client_id=google_api_keys["CLIENT_ID"], client_secret=google_api_keys["CLIENT_SECRET"], scope="https://www.googleapis.com/auth/userinfo.email", user_agent="Galah" ) storage = Storage(config["galah_home"] + "/tmp/oauth_credentials") # Get new credentials from user credentials = run(flow, storage) # If the credentials are authorized, they've given permission. http = httplib2.Http() http = credentials.authorize(http) # Extract email and email verification id_token = credentials.id_token verified_email = id_token["verified_email"] access_token = credentials.access_token if id_token["email"] != user: print >> sys.stderr, ( "You are trying to act as %s however google authenticated you as " "%s. Change your configuration to match your google account or use " "the -u option." % (user, id_token["email"]) ) exit(1) if verified_email != "true": raise RuntimeError("Could not verify email") request = session.post( config["galah_host"] + "/api/login", data = { "access_token": access_token } ) request.raise_for_status() # Check if we successfully logged in. if request.headers["X-CallSuccess"] != "True": raise RuntimeError(request.text)
from apiclient.discovery import build from httplib2 import Http from oauth2client import file, client, tools SCOPES = "" CLIENT_SECRET = "client_secret.json" store = file.Storage("storage.json") credz = store.get() if not credz or credz.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET) credz = tools.run(flow, store) SERVICE = build(API, VERSION, http=credz.authorize(Http()))
def get_from_flow(creds, storage): if creds is None or creds.invalid: self.credentials = run(FLOW, storage)
def main(): """Perform OAuth 2 authorization, then start, list, and stop instance(s).""" logging.basicConfig(level=logging.INFO) # Load the settings for this sample app. settings = json.loads(open(gce.SETTINGS_FILE, 'r').read()) # Perform OAuth 2.0 authorization flow. flow = flow_from_clientsecrets(settings['client_secrets'], scope=settings['compute_scope']) storage = Storage(settings['oauth_storage']) credentials = storage.get() # Authorize an instance of httplib2.Http. if credentials is None or credentials.invalid: credentials = run(flow, storage) http = httplib2.Http() auth_http = credentials.authorize(http) # Retrieve user input. image_url = raw_input('Enter the URL of an image [Defaults to %s]: ' % IMAGE_URL) if not image_url: image_url = IMAGE_URL image_text = raw_input( 'Enter text to add to the image [Defaults to "%s"]: ' % IMAGE_TEXT) if not image_text: image_text = IMAGE_TEXT bucket = raw_input('Enter a Cloud Storage bucket [Required]: ') if not bucket: logging.error('Cloud Storage bucket required.') return # Initialize gce.Gce. gce_helper = gce.Gce(auth_http, project_id=settings['project']) # Create a Persistent Disk (PD), which is used as a boot disk. try: gce_helper.create_disk(DISK_NAME) except (gce.ApiError, gce.ApiOperationError, ValueError, Exception) as e: logging.error(INSERT_ERROR, {'name': DISK_NAME}) logging.error(e) return # Start an instance with a local start-up script and boot disk. logging.info('Starting GCE instance') try: gce_helper.start_instance( INSTANCE_NAME, DISK_NAME, service_email=settings['compute']['service_email'], scopes=settings['compute']['scopes'], startup_script='startup.sh', metadata=[{ 'key': 'url', 'value': image_url }, { 'key': 'text', 'value': image_text }, { 'key': 'cs-bucket', 'value': bucket }]) except (gce.ApiError, gce.ApiOperationError, ValueError, Exception) as e: # Delete the disk in case the instance fails to start. delete_resource(gce_helper.delete_disk, DISK_NAME) logging.error(INSERT_ERROR, {'name': INSTANCE_NAME}) logging.error(e) return except gce.DiskDoesNotExistError as e: logging.error(INSERT_ERROR, {'name': INSTANCE_NAME}) logging.error(e) return # List all running instances. logging.info('These are your running instances:') instances = gce_helper.list_instances() for instance in instances: logging.info(instance['name']) logging.info('Visit http://storage.googleapis.com/%s/output.png.' % bucket) logging.info('It might take a minute for the output.png file to show up.') raw_input('Hit Enter when done to shutdown instance.') # Stop the instance. delete_resource(gce_helper.stop_instance, INSTANCE_NAME) # Delete the disk. delete_resource(gce_helper.delete_disk, DISK_NAME) logging.info('Remember to delete the output.png file in ' + bucket)
def get_from_flow(creds, storage): self.credentials = run(FLOW, storage)
from oauth2client.client import flow_from_clientsecrets, Storage, EXPIRY_FORMAT from oauth2client.tools import run gflags.DEFINE_string( 'secrets', None, 'Client secret JSON filename', short_name='f') gflags.FLAGS(sys.argv) CLIENT_SECRETS = gflags.FLAGS.secrets MISSING_CLIENT_SECRETS_MESSAGE = ("%s is missing or doesn't contain secrets " "for a web application" % CLIENT_SECRETS) FLOW = flow_from_clientsecrets( CLIENT_SECRETS, scope='https://www.googleapis.com/auth/analytics.readonly', message=MISSING_CLIENT_SECRETS_MESSAGE) s = Storage() s.put = lambda *a, **kw: None credentials = run(FLOW, s) bits = dict([(str(name), str(getattr(credentials, name))) for name in ('access_token', 'client_id', 'client_secret', 'refresh_token', 'token_uri', 'user_agent')]) bits['token_expiry'] = credentials.token_expiry.strftime(EXPIRY_FORMAT) print 'GOOGLE_ANALYTICS_CREDENTIALS = ', pprint.pprint(bits)
def main(argv): option_parser = OptionParser() option_parser.add_option('-c', '--create-table', action='store_true') option_parser.add_option('-t', '--table-name', default='devfestzurich', metavar='TABLE') option_parser.add_option('--table-id', default='1O7qsDkkgaDbAArUKywHVwPxVqz4RA9P1xEAfrHU', metavar='TABLE-ID') option_parser.add_option('-q', '--query', metavar='QUERY') option_parser.add_option('-k', '--key', default='AIzaSyBfoMH8qNEQYBjLA9u0jLgs5V6o7KiAFbQ', metavar='KEY') option_parser.add_option('-l', '--limit', default=20, metavar='LIMIT', type=int) option_parser.add_option('-f', '--fields', default='items(geocode,id,published),nextPageToken', metavar='FIELDS') option_parser.add_option('--client-id', default='265903001164-lrjmvjnqjl2sfa13ogofm3hj2roakqkj.apps.googleusercontent.com') option_parser.add_option('--client-secret', default='DgdHp4OsayYhTx3kPhXTYt1W') option_parser.add_option('--scope', default='https://www.googleapis.com/auth/fusiontables') option_parser.add_option('--log-level', default=0, type=int) option_parser.add_option('--verbose', '-v', action='count', dest='log_level') options, args = option_parser.parse_args(argv[1:]) logging.basicConfig(format='%(asctime)s %(message)s', level=logging.WARN - 10 * options.log_level) logger = logging.getLogger(os.path.basename(argv[0])) flow = OAuth2WebServerFlow(options.client_id, options.client_secret, options.scope) storage = Storage('credentials.dat') credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(flow, storage) plus_session = requests.session() ft_session = requests.session(headers={'Authorization': 'OAuth ' + credentials.access_token}) if options.create_table: data = { 'description': 'DevFest Zurich', 'isExportable': True, 'name': options.table_name, 'columns': [ {'name': 'id', 'type': 'STRING'}, {'name': 'published', 'type': 'DATETIME'}, {'name': 'query', 'type': 'STRING'}, {'name': 'geocode', 'type': 'LOCATION'}, ], } while True: ft_response = ft_session.post('https://www.googleapis.com/fusiontables/v1/tables', data=json.dumps(data), headers={'Content-Type': 'application/json'}) if ft_response.status_code == 200: break elif ft_response.status_code == 401: credentials = run(flow, storage) else: print repr(ft_response) print repr(ft_response.content) raise RuntimeError print repr(ft_response) print repr(ft_response.json) params = { 'fields': options.fields, 'key': options.key, 'maxResults': 20, 'query': options.query, } count = 0 while options.limit and count < options.limit: plus_response = plus_session.get('https://www.googleapis.com/plus/v1/activities', params=params) logging.info('got %d items, %d with geocode' % (len(plus_response.json['items']), len([item for item in plus_response.json['items'] if 'geocode' in item]))) items = [Item(item_json) for item_json in plus_response.json['items'] if 'geocode' in item_json] for item in items: while True: time.sleep(2) ft_response = ft_session.post('https://www.googleapis.com/fusiontables/v1/query', headers={'Content-Length': '0'}, params={'sql': item.insert(options.table_id, options.query)}) #print repr(ft_response) #print repr(ft_response.content) if ft_response.status_code == 200: break elif ft_response.status_code == 401: credentials = run(flow, storage) else: print repr(ft_response) print repr(ft_response.content) raise RuntimeError count += len(items) if 'nextPageToken' in plus_response.json: params['pageToken'] = plus_response.json['nextPageToken'] else: break
from oauth2client.client import OAuth2WebServerFlow from oauth2client.tools import run from oauth2client.file import Storage CLIENT_ID = '202379785889-9ln68an8pr9o7ldlckaev64bjej76mnm.apps.googleusercontent.com' CLIENT_SECRET = '7bf431wrB0zWxGyhDTRonKXl' flow = OAuth2WebServerFlow( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope='https://spreadsheets.google.com/feeds https://docs.google.com/feeds', redirect_uri='http://localhost:8080/') storage = Storage('creds.data') credentials = run(flow, storage) print "access_token: %s" % credentials.access_token
def seed (db): #establish cursor, update tld data cursor = db.cursor() update_tld_names() domain = "" #insert sites from seed and safeSeed csv files with open('seed.csv', 'rb') as csvfile: seedReader = csv.reader(csvfile, delimiter=',') for link in seedReader: link = link[0] if get_tld(link, fail_silently=True) != None: print "ADDING %s TO SPAM SEED... \n" % link domain = get_tld(link, fail_silently=True) try: execString = ("INSERT IGNORE INTO seed(Domain, URL, URLSource, Crawled) VALUES ('%s', '%s', 'list', '0');" %(domain, link)) cursor.execute(execString) db.commit() except: print ("FAILED TO EXECUTE SQL QUERY: %s" %execString) with open('safeSeed.csv', 'rb') as csvfile: seedReader = csv.reader(csvfile, delimiter=',') for link in seedReader: link = link[0] if get_tld(link, fail_silently=True) != None: print "ADDING %s TO SAFE SEED... \n" % link domain = get_tld(link, fail_silently=True) try: execString = ("INSERT IGNORE INTO safeSeed(Domain, URL, URLSource, Crawled) VALUES ('%s', '%s', 'list', '0');" %(domain, link)) cursor.execute(execString) db.commit() except: print ("FAILED TO EXECUTE SQL QUERY: %s" %execString) try: #get the whitelist from the sql server execString = ("SELECT Domain FROM WhiteList;") cursor.execute(execString) wl = list(cursor) #use a file user.json in this directory to log into Gmail and pull down spam flow = flow_from_clientsecrets('user.json', scope='https://www.googleapis.com/auth/gmail.readonly') http = httplib2.Http() credentials = 'gmail.storage'.get() if credentials is None or credentials.invalid: credentials = run(flow, 'gmail.storage', http=http) http = credentials.authorize(http) gmail_service = build('gmail', 'v1', http=http) spamMsgs = gmail_service.users().messages().list(userId='me', labelIds='SPAM').execute() execString = "" i=0 except: print ("Unable to read spam email. You need user.json gmail credentials in this directory.") for spam in spamMsgs['messages']: i = i+1 try: print spam messageId =(spam['id']) message = gmail_service.users().messages().get(id=messageId, userId='me').execute() stringe = (message['payload']['body']) for part in message['payload']['parts']: content = part['body']['data'] content = base64.urlsafe_b64decode(content.encode('ascii')) for url in re.findall('''http["'](.[^"']+)["']''', content): try: domainTo = (url.split("/"))[2] if ((domain + "/") in wl): print ("Whitelisted \n") bad = 0 else: bad =1 execString = ("INSERT IGNORE INTO seed (Domain, URL, URLSource, crawled) VALUES ('%s', '%s', 'list', 0);" % (domain, url)) cursor.execute(execString) except: print "Failed to add this piece of spam" content=db.escape_string(content) execString = ("INSERT INTO Content (Lvl, Content, Domain, URL, CopySource) VALUES ('0', '%s', '%i', '%s', 'email');" % (content, i, str(messageId))) cursor.execute(execString) db.commit() except Exception as e: print ("Failed to load email: %s" %execString) print (type(e)) print (e.args) db.close()
except auto_auth.CredentialsError, e: # We failed to get the scopes from the metadata server. if logger: logger.warn( 'Failed to automatically authenticate with service ' 'account: %s' % (e)) storage = oauth2_multistore_file.get_credential_storage( credentials_file, client_id, user_agent, scopes_str) credentials = storage.get() if force_reauth and credentials: credentials.invalid = True if (not credentials or credentials.invalid == True) and ask_user: if FLAGS.auth_service_account and metadata_present: print( 'Service account scopes are not enabled for %s on this instance. ' 'Using manual authentication.') % (FLAGS.auth_service_account) flow = oauth2_client.OAuth2WebServerFlow( client_id=client_id, client_secret=client_secret, scope=scopes_str, user_agent=user_agent, auth_uri=authorization_uri_base + '/auth', token_uri=authorization_uri_base + '/token') credentials = oauth2_tools.run(flow, storage) return credentials