src/foggycam.py
def initialize_session(self):
        """Creates the first session to get the access token and cookie."""

        print ('INFO: Initializing session...')

        payload = {'email':self.nest_username, 'password':self.nest_password}
        binary_data = json.dumps(payload).encode('utf-8')

        request = urllib.request.Request(self.nest_session_url, binary_data)
        request.add_header('Content-Type', 'application/json')

        try:
            response = self.merlin.open(request)
            session_data = response.read()
            session_json = json.loads(session_data)

            self.nest_access_token = session_json['access_token']
            self.nest_access_token_expiration = session_json['expires_in']
            self.nest_user_id = session_json['userid']

            print ('INFO: [PARSED] Captured authentication token:')
            print (self.nest_access_token)

            print ('INFO: [PARSED] Captured expiration date for token:')
            print (self.nest_access_token_expiration)

            cookie_data = dict((cookie.name, cookie.value) for cookie in self.cookie_jar)
            for cookie in cookie_data:
                print (cookie)

            print ('INFO: [COOKIE] Captured authentication token:')
            print (cookie_data["cztoken"])
        except urllib.request.HTTPError as err:
            if err.code == 401:
                error_message = err.read()
                unauth_content = json.loads(error_message)

                if unauth_content["status"].lower() == "verification_pending":
                    print ("Pending 2FA verification!")

                    two_factor_token = unauth_content["2fa_token"]
                    phone_truncated = unauth_content["truncated_phone_number"]

                    print ("Enter PIN you just received on number ending with", phone_truncated)
                    pin = input()

                    payload = {"pin":pin ,"2fa_token":two_factor_token}
                    binary_data = json.dumps(payload).encode('utf-8')

                    request = urllib.request.Request(self.nest_verify_pin_url, binary_data)
                    request.add_header('Content-Type', 'application/json')

                    try:
                        response = self.merlin.open(request)
                        pin_attempt = response.read()

                        parsed_pin_attempt = json.loads(pin_attempt)
                        if parsed_pin_attempt["status"].lower() == "id_match_positive":
                            print ("2FA verification successful.")

                            utc_date = datetime.utcnow()
                            utc_millis_str = str(int(utc_date.timestamp())*1000)
                            
                            print ("Targetting new session with timestamp: ", utc_millis_str)
                            
                            cookie_data = dict((cookie.name, cookie.value) for cookie in self.cookie_jar)

                            print ('INFO: [COOKIE] Captured authentication token:')
                            print (cookie_data["cztoken"])

                            self.nest_access_token = parsed_pin_attempt['access_token']

                            self.initialize_twof_session(utc_millis_str)
                        else:
                            print ("Could not verify. Exiting...")
                            exit()
                    
                    except:
                        traceback.print_exc()

                        print ("Failed 2FA checks. Exiting...")
                        exit()

        print ('INFO: Session initialization complete!')
Creates the first session to get the access token and cookie.
Creates the first session to get the access token and cookie.
Creates the first session to get the access token and cookie .
def initialize_session(self):
        print ('INFO: Initializing session...')
        payload = {'email':self.nest_username, 'password':self.nest_password}
        binary_data = json.dumps(payload).encode('utf-8')
        request = urllib.request.Request(self.nest_session_url, binary_data)
        request.add_header('Content-Type', 'application/json')
        try:
            response = self.merlin.open(request)
            session_data = response.read()
            session_json = json.loads(session_data)
            self.nest_access_token = session_json['access_token']
            self.nest_access_token_expiration = session_json['expires_in']
            self.nest_user_id = session_json['userid']
            print ('INFO: [PARSED] Captured authentication token:')
            print (self.nest_access_token)
            print ('INFO: [PARSED] Captured expiration date for token:')
            print (self.nest_access_token_expiration)
            cookie_data = dict((cookie.name, cookie.value) for cookie in self.cookie_jar)
            for cookie in cookie_data:
                print (cookie)
            print ('INFO: [COOKIE] Captured authentication token:')
            print (cookie_data["cztoken"])
        except urllib.request.HTTPError as err:
            if err.code == 401:
                error_message = err.read()
                unauth_content = json.loads(error_message)
                if unauth_content["status"].lower() == "verification_pending":
                    print ("Pending 2FA verification!")
                    two_factor_token = unauth_content["2fa_token"]
                    phone_truncated = unauth_content["truncated_phone_number"]
                    print ("Enter PIN you just received on number ending with", phone_truncated)
                    pin = input()
                    payload = {"pin":pin ,"2fa_token":two_factor_token}
                    binary_data = json.dumps(payload).encode('utf-8')
                    request = urllib.request.Request(self.nest_verify_pin_url, binary_data)
                    request.add_header('Content-Type', 'application/json')
                    try:
                        response = self.merlin.open(request)
                        pin_attempt = response.read()
                        parsed_pin_attempt = json.loads(pin_attempt)
                        if parsed_pin_attempt["status"].lower() == "id_match_positive":
                            print ("2FA verification successful.")
                            utc_date = datetime.utcnow()
                            utc_millis_str = str(int(utc_date.timestamp())*1000)
                            print ("Targetting new session with timestamp: ", utc_millis_str)
                            cookie_data = dict((cookie.name, cookie.value) for cookie in self.cookie_jar)
                            print ('INFO: [COOKIE] Captured authentication token:')
                            print (cookie_data["cztoken"])
                            self.nest_access_token = parsed_pin_attempt['access_token']
                            self.initialize_twof_session(utc_millis_str)
                        else:
                            print ("Could not verify. Exiting...")
                            exit()
                    except:
                        traceback.print_exc()
                        print ("Failed 2FA checks. Exiting...")
                        exit()
        print ('INFO: Session initialization complete!')
def initialize_session ( self ) : print ( 'INFO: Initializing session...' ) payload = { 'email' : self . nest_username , 'password' : self . nest_password } binary_data = json . dumps ( payload ) . encode ( 'utf-8' ) request = urllib . request . Request ( self . nest_session_url , binary_data ) request . add_header ( 'Content-Type' , 'application/json' ) try : response = self . merlin . open ( request ) session_data = response . read ( ) session_json = json . loads ( session_data ) self . nest_access_token = session_json [ 'access_token' ] self . nest_access_token_expiration = session_json [ 'expires_in' ] self . nest_user_id = session_json [ 'userid' ] print ( 'INFO: [PARSED] Captured authentication token:' ) print ( self . nest_access_token ) print ( 'INFO: [PARSED] Captured expiration date for token:' ) print ( self . nest_access_token_expiration ) cookie_data = dict ( ( cookie . name , cookie . value ) for cookie in self . cookie_jar ) for cookie in cookie_data : print ( cookie ) print ( 'INFO: [COOKIE] Captured authentication token:' ) print ( cookie_data [ "cztoken" ] ) except urllib . request . HTTPError as err : if err . code == 401 : error_message = err . read ( ) unauth_content = json . loads ( error_message ) if unauth_content [ "status" ] . lower ( ) == "verification_pending" : print ( "Pending 2FA verification!" ) two_factor_token = unauth_content [ "2fa_token" ] phone_truncated = unauth_content [ "truncated_phone_number" ] print ( "Enter PIN you just received on number ending with" , phone_truncated ) pin = input ( ) payload = { "pin" : pin , "2fa_token" : two_factor_token } binary_data = json . dumps ( payload ) . encode ( 'utf-8' ) request = urllib . request . Request ( self . nest_verify_pin_url , binary_data ) request . add_header ( 'Content-Type' , 'application/json' ) try : response = self . merlin . open ( request ) pin_attempt = response . read ( ) parsed_pin_attempt = json . loads ( pin_attempt ) if parsed_pin_attempt [ "status" ] . lower ( ) == "id_match_positive" : print ( "2FA verification successful." ) utc_date = datetime . utcnow ( ) utc_millis_str = str ( int ( utc_date . timestamp ( ) ) * 1000 ) print ( "Targetting new session with timestamp: " , utc_millis_str ) cookie_data = dict ( ( cookie . name , cookie . value ) for cookie in self . cookie_jar ) print ( 'INFO: [COOKIE] Captured authentication token:' ) print ( cookie_data [ "cztoken" ] ) self . nest_access_token = parsed_pin_attempt [ 'access_token' ] self . initialize_twof_session ( utc_millis_str ) else : print ( "Could not verify. Exiting..." ) exit ( ) except : traceback . print_exc ( ) print ( "Failed 2FA checks. Exiting..." ) exit ( ) print ( 'INFO: Session initialization complete!' )
Creates the first session to get the access token and cookie.
Creates the first session to get the access token and cookie .
"""Creates the first session to get the access token and cookie."""
self