How to use the oauth2client.file function in oauth2client

To help you get started, we’ve selected a few oauth2client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jpmens / mqttwarn / mqttwarn / services / gss2.py View on Github external
oauth2_storage_filename = item.config['oauth2_storage_filename']
    except KeyError as e:
        srv.logging.error("Some configuration item is missing: %s" % str(e))
        return False

    if not os.path.exists(client_secrets_filename):
        srv.logging.error("Cannot find file '%s'." % client_secrets)
        return False

    try:
        srv.logging.debug("Adding row to spreadsheet %s [%s]..." % (spreadsheet_url, worksheet_name))
        if os.path.isfile(oauth2_storage_filename):
            # Valid credentials from previously completed authentication?
            srv.logging.debug("Trying to use credentials from file '%s'." %
                oauth2_storage_filename)
            storage = oauth2client.file.Storage(oauth2_storage_filename)
            credentials = storage.get()
            if credentials is None or credentials.invalid:
                srv.logging.error("Error reading credentials from file '%s'." %
                    oauth2_storage_filename)
                return False
        elif oauth2_code is not None and len(oauth2_code) > 0:
            # After restart - hopefully with the code coming from the Google webpage.
            srv.logging.debug("Trying to use client_secrets from '%s' and OAuth code '%s'." %
                (client_secrets_filename, oauth2_code))
            try:
                credentials = oauth2client.client.credentials_from_clientsecrets_and_code(
                    client_secrets_filename,
                    scope=SCOPE,
                    code=oauth2_code,
                    redirect_uri='urn:ietf:wg:oauth:2.0:oob')
                if credentials is None:
github sandeshghanta / Status_Update_Bot / gmail_api.py View on Github external
def getdata(date):
    store = file.Storage('/home/sandeshghanta/mysite/token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    threadName = '[foss-2018] Status Update [' + date + ']'
    user_id = "me"
    messages = list_messages_matching_query(service, user_id='me', query=threadName)
    email_to_content = {}
    for message in messages:
        print (message)
        email_id,content = get_sender_email_id_and_maildata(service, user_id="me", msg_id=message['id'])
        email_to_content[email_id] = content
    return email_to_content
github robert8138 / flask-google-calendar-api-project / src / models / calendarapi.py View on Github external
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
github tommybobbins / PiThermostat / google_calendar.py View on Github external
def google_calendar():
  # 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 the file.
  storage = file.Storage('/etc/google_calendar/sample.dat')
  credentials = storage.get()
  return_temp = 13.6654
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(FLOW, storage, flags)

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with our good Credentials.
  http = httplib2.Http()
  http = credentials.authorize(http)

  # Construct the service object for the interacting with the Calendar API.
  service = discovery.build('calendar', 'v3', http=http)

  try:
#    print "Success! Now add code here."
    baildon_tz = pytz.timezone('Europe/London')
github astorfi / pythonic-automatic-email / code / sendmessage.py View on Github external
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-email-send.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
        credentials = tools.run_flow(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials
github tommybobbins / PiThermostat / check_cal.py View on Github external
def google_calendar():
  # 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 the file.
    return_temp = 13.666
    storage = file.Storage('sample.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid:
      credentials = tools.run_flow(FLOW, storage, flags)

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with our good Credentials.
    http = httplib2.Http()
    http = credentials.authorize(http)

  # Construct the service object for the interacting with the Calendar API.
    service = discovery.build('calendar', 'v3', http=http)

#    print "Success! Now add code here."
    baildon = pytz.timezone('Europe/London')
    now = datetime.now(tz=baildon) # timezone?
#    timeMin = datetime(year=now.year, month=now.month, day=now.day, hour=now.hour, minute=now.minute, tzinfo=baildon)
github utkuufuk / budget-cli / budget / __main__.py View on Github external
def getSheetService():
    os.chdir(APP_DIR)
    store = file.Storage('token.json')
    creds = store.get()
    return build('sheets', 'v4', http=creds.authorize(Http())).spreadsheets().values()
github GoogleCloudPlatform / gce-oreilly / ch7-6.py View on Github external
def main(argv):
  # Parse the command-line flags.
  flags = parser.parse_args(argv[1:])

  # 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 the file.
  storage = file.Storage('sample.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(FLOW, storage, flags)

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with our good Credentials.
  http = httplib2.Http()
  http = credentials.authorize(http)

  # Construct the service object for the interacting with the Compute Engine API.
  service = discovery.build('compute', 'v1', http=http)

  # print 'Success! Now add code here.'

  # Set project, zone, and other constants.
  URL_PREFIX = 'https://www.googleapis.com/compute'
github mesozoic / gmail-yaml-filters / gmail_yaml_filters / upload.py View on Github external
def get_gmail_credentials(
    scopes=[
        'https://www.googleapis.com/auth/gmail.settings.basic',
        'https://www.googleapis.com/auth/gmail.labels',
    ],
    client_secret_path='client_secret.json',
    application_name='gmail_yaml_filters',
):
    credential_dir = os.path.join(os.path.expanduser('~'), '.credentials')
    credential_path = os.path.join(credential_dir, application_name + '.json')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = oauth2client.client.flow_from_clientsecrets(client_secret_path, scopes)
        flow.user_agent = application_name
        flags_parser = argparse.ArgumentParser(parents=[oauth2client.tools.argparser])
        credentials = oauth2client.tools.run_flow(flow, store, flags=flags_parser.parse_args([]))
        print('Storing credentials to', credential_path, file=sys.stderr)

    return credentials
github Ajithkumarsekar / KCT-Academic-Calendar-Converter / KCT-Calendar / CalendarApi.py View on Github external
def create_remainder(name,date):
	
	#REFERENCE:https://developers.google.com/google-apps/calendar/v3/reference/events/insert#examples
	print name+" "+date
	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()))
	 
	#Events in JSON format
	ID="fokql1u8cutplmdfeu6ql4hqs8@group.calendar.google.com"
	EVENT={
	'summary':name,
	'start':{'dateTime':date+'T7:00:00+05:30'},
	'end':  {'dateTime':date+'T8:00:00+05:30'}
		  }