def connect():
    """Store the user's data and finish signing them in."""

    # Ensure that the request is not a forgery and that the user sending
    # this connect request is the expected user.
    if request.args.get('state') != session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Auth code to credentials object
    try:
        user_credentials = _oauth_flow().step2_exchange(request.data)
    except FlowExchangeError:
        response = make_response(json.dumps('Failed to upgrade the authorization code.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    user_id = user_credentials.id_token['sub']
    stored_credentials = session.get('credentials')
    stored_userid = session.get('user_id')

    if stored_credentials is not None and user_id == stored_userid:
        response = make_response(json.dumps('Current user is already connected.'), 200)
        response.headers['Content-Type'] = 'application/json'
        return response

    session['credentials'] = user_credentials
    session['user_id'] = user_id

    # Check the datastore for the user
    if _credentials_for_user(user_id).get() is None:
        # Store the user's credentials so you can use them even after this
        # step is done.
        _credentials_for_user(user_id).put(user_credentials)

        # Sample user properties that we might want to give a user
        # Example: do they want email, do they want cards on the weekends
        user_properties = UserProperties(id=session['user_id'], email=False, weekends=False)
        user_properties.put()

        # Create a an auth'ed Mirror API serivce
        mirror_service = _authorized_mirror_service(user_credentials)

        mirror_service.timeline().insert(body={
            'notification': {'level': 'DEFAULT'},
            'text': 'Welcome to Glass Daily Card!',
            'menuItems': [{'action': 'DELETE'}]}
        ).execute()

    # Create a response that's all good
    response = make_response(json.dumps('Successfully connected user.', 200))
    response.headers['Content-Type'] = 'application/json'
    return response
Exemple #2
0
def dailyjob():
    """This is run by the cron job. Check cron.yaml to see when."""

    # Get today's card
    today = datetime.date.today()
    query_get_todays_card = CronCards.query(CronCards.date == today)

    # Fetch one result from the query
    result_get_todays_card = query_get_todays_card.get()

    # Do we have a card to send today?
    if result_get_todays_card is not None:

        # Setup our Timeline Card body
        # We assume for example's sake that it's text
        timelinecard_body = {
            'notification': {
                'level': 'DEFAULT'
            },
            'text': result_get_todays_card.card,
            'menuItems': [{
                'action': 'DELETE'
            }]
        }

        #
        # This is the most basic way possible to do this.
        # If you're running large numbers of users, you should be batching
        # to cut your HTML:
        # https://developers.google.com/glass/batch
        #
        query_get_users = UserProperties.query()
        for user in query_get_users.fetch():
            #
            # Why UserProperties? Because you should not be spamming your
            # users on Glass if they don't want cards!
            #
            # Define your properties, make Glassware better.
            #

            # Get the credentials for the user
            user_credentials = _credentials_for_user(user.key.id()).get()

            # Send a card
            _authorized_mirror_service(user_credentials).timeline().insert(
                body=timelinecard_body).execute()

    response = make_response("{}", 200)
    response.headers['Content-Type'] = 'application/json'
    return response
def dailyjob():
    """This is run by the cron job. Check cron.yaml to see when."""

    # Get today's card
    today = datetime.date.today()
    query_get_todays_card = CronCards.query(CronCards.date == today)

    # Fetch one result from the query
    result_get_todays_card = query_get_todays_card.get()

    # Do we have a card to send today?
    if result_get_todays_card is not None:
            
        # Setup our Timeline Card body
        # We assume for example's sake that it's text
        timelinecard_body = {
            'notification': {'level': 'DEFAULT'},
            'text': result_get_todays_card.card,
            'menuItems': [{'action': 'DELETE'}]
        }

        #
        # This is the most basic way possible to do this.
        # If you're running large numbers of users, you should be batching
        # to cut your HTML:
        # https://developers.google.com/glass/batch
        #
        query_get_users = UserProperties.query()
        for user in query_get_users.fetch():
            #
            # Why UserProperties? Because you should not be spamming your
            # users on Glass if they don't want cards!
            #
            # Define your properties, make Glassware better.
            #

            # Get the credentials for the user
            user_credentials = _credentials_for_user(user.key.id()).get()

            # Send a card
            _authorized_mirror_service(user_credentials).timeline().insert(body=timelinecard_body).execute()

    response = make_response("{}", 200)
    response.headers['Content-Type'] = 'application/json'
    return response
def connect():
    """Store the user's data and finish signing them in."""

    # Ensure that the request is not a forgery and that the user sending
    # this connect request is the expected user.
    if request.args.get('state') != session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Auth code to credentials object
    try:
        user_credentials = _oauth_flow().step2_exchange(request.data)
    except FlowExchangeError:
        response = make_response(json.dumps('Failed to upgrade the authorization code.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    user_id = user_credentials.id_token['sub']
    stored_credentials = session.get('credentials')
    stored_userid = session.get('user_id')

    if stored_credentials is not None and user_id == stored_userid:
        response = make_response(json.dumps('Current user is already connected.'), 200)
        response.headers['Content-Type'] = 'application/json'
        return response

    session['credentials'] = user_credentials
    session['user_id'] = user_id

    # Check the datastore for the user
    if _credentials_for_user(user_id).get() is None:
        # Store the user's credentials so you can use them even after this
        # step is done.
        _credentials_for_user(user_id).put(user_credentials)

        # Sample user properties that we might want to give a user
        # Example: do they want email, do they want cards on the weekends
        user_properties = UserProperties(id=session['user_id'], email=False, weekends=False)
        user_properties.put()

        # Create a an auth'ed Mirror API serivce
        mirror_service = _authorized_mirror_service(user_credentials)

        # Subscribe to image shares from the contact which was just created.
        mirror_service.subscriptions().insert(body={
                   'collection': 'timeline',
                   'userToken': user_id,
                   'verifyToken': 'something_only_you_know',
                   'callbackUrl': 'https://my-project-id.appspot.com/glassCallback',
                   'operation': ['UPDATE']}).execute()

        mirror_service.timeline().insert(body={
            'notification': {'level': 'DEFAULT'},
            'text': 'Welcome to Glass Task Future! Choose Random Ping from menu for future card.',
            'menuItems': [
                {'action': 'CUSTOM', 'id': 'random-ping-ahhhh', 'values': [ { 'displayName': "Random Ping!" } ] },
                {'action': 'DELETE'}] 
            }).execute()

    # Create a response that's all good
    response = make_response(json.dumps('Successfully connected user.', 200))
    response.headers['Content-Type'] = 'application/json'
    return response
Exemple #5
0
def connect():
    """Store the user's data and finish signing them in."""

    # Ensure that the request is not a forgery and that the user sending
    # this connect request is the expected user.
    if request.args.get('state') != session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Auth code to credentials object
    try:
        user_credentials = _oauth_flow().step2_exchange(request.data)
    except FlowExchangeError:
        response = make_response(
            json.dumps('Failed to upgrade the authorization code.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    user_id = user_credentials.id_token['sub']
    stored_credentials = session.get('credentials')
    stored_userid = session.get('user_id')

    if stored_credentials is not None and user_id == stored_userid:
        response = make_response(
            json.dumps('Current user is already connected.'), 200)
        response.headers['Content-Type'] = 'application/json'
        return response

    session['credentials'] = user_credentials
    session['user_id'] = user_id

    # Check the datastore for the user
    if _credentials_for_user(user_id).get() is None:
        # Store the user's credentials so you can use them even after this
        # step is done.
        _credentials_for_user(user_id).put(user_credentials)

        # Sample user properties that we might want to give a user
        # Example: do they want email, do they want cards on the weekends
        user_properties = UserProperties(id=session['user_id'],
                                         email=False,
                                         weekends=False)
        user_properties.put()

        # Create a an auth'ed Mirror API serivce
        mirror_service = _authorized_mirror_service(user_credentials)

        mirror_service.timeline().insert(
            body={
                'notification': {
                    'level': 'DEFAULT'
                },
                'text': 'Welcome to Glass Daily Card!',
                'menuItems': [{
                    'action': 'DELETE'
                }]
            }).execute()

    # Create a response that's all good
    response = make_response(json.dumps('Successfully connected user.', 200))
    response.headers['Content-Type'] = 'application/json'
    return response